Tola
Tola

Reputation: 2421

Cast String to 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)

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.

Upvotes: 0

Views: 24157

Answers (2)

Svetlozar Angelov
Svetlozar Angelov

Reputation: 21660

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)

Upvotes: 6

LukeH
LukeH

Reputation: 269298

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

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

Upvotes: 0

Related Questions