开发者

Cast String to TimeSpan

开发者 https://www.devze.com 2022-12-13 11:38 出处:网络
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\")

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)

It threw error like this:

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.


The parameter for TimeSpan.Parse must be in format hh:mm:ss, not hh:mm:ff

The format is [ws][-][d.]hh:mm:ss[.ff][ws]

hh:mm:ss are required, the others are optional

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


Are you really trying to parse hours, months and fractions of seconds?

Your format string should probably be something like HH:mm:ss instead.

0

精彩评论

暂无评论...
验证码 换一张
取 消