将字符串转换为 TimeSpan [英] Cast String to TimeSpan

查看:43
本文介绍了将字符串转换为 TimeSpan的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试将字符串解析为 TimeSpan,如下所示:

I tried to parse string to TimeSpan like the following :

    Dim dt As DateTime = DateTime.Now
    Dim timeCheckin As String = Format(dt, "HH:MM:FF")
    ts = TimeSpan.Parse(timeCheckin)

它抛出了这样的错误:

System.OverflowException:无法解析 TimeSpan,因为小时、分钟或秒组件中的至少一个超出其有效范围.

System.OverflowException: The TimeSpan could not be parsed because at least one of the hours, minutes, or seconds components is outside its valid range.

谁能给我一个建议?谢谢.

Can anyone give me a suggestion? Thank you.

推荐答案

TimeSpan.Parse 必须采用 hh:mm:ss 格式,而不是 hh:mm:ff>

格式为[ws][-][d.]hh:mm:ss[.ff][ws]

hh:mm:ss 为必填项,其他可选

Dim timeCheckin As String = Format(dt, "HH:mm:ss")
ts = TimeSpan.Parse(timeCheckin)

这篇关于将字符串转换为 TimeSpan的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆