tags:

views:

235

answers:

2

Hi,

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.

A: 

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

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

LukeH
+1  A: 

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)
Svetlozar Angelov
Put a breakpoint after the TimeSpan.Parse and investigate the ts variable. It has all properties - Minutes, Hours and etc... You will find out if the ts has correct value
Svetlozar Angelov
Sorry for the trouble. Thank you.
Angkor Wat
`MM` is months. You need lowercase `mm` for minutes.
LukeH