Convert string to timespan

Hi!
I want to convert myString - 60:00:00 to timespan
when I try TimeSpan.Parse(myString) the output is - 60.00:00:00
not sure why it is that format, need it to be just the 60 hours 0 minutes and 0 seconds
Can anyone help me with this?

Thanks in advance! :slight_smile:

Hi @mimuhrin

The hours component, must be between 0 and 23. Because of this, passing β€œ23:00:00” to the Parse method returns a time interval of 23 hours. On the other hand, passing β€œ24:00:00” returns a time interval of 24 days. Because β€œ24” is outside the range of the hours component, it is interpreted as the days component.
So if you want just 60 hours, you need to try TimeSpan.Parse(β€œ2.12:00:00”).

Hi,

TimeSpan.Parse method fails greater 24 hour, because TimeSpan variable cannot have hour over 24, as @wusiyangjia mentioned.

Can you try the following?

text = "60:00:00"
match = System.Text.RegularExpressions.Regex.Match(text,"(\d+):(\d+):(\d+)")
ts = New TimeSpan(0,Int32.Parse(match.Groups(1).Value),Int32.Parse(match.Groups(2).Value),Int32.Parse(match.Groups(3).Value))

if you want to get 60 (hour), ts.Hours+ts.Days*24 returns it.

Main.xaml (6.8 KB)

Regards,

Check this below code, @mimuhrin
Split(β€œ60:00:00”,β€œ:”)(0)+" hours β€œ+Split(β€œ60:00:00”,”:β€œ)(1)+” minutes β€œ+Split(β€œ60:00:00”,”:β€œ)(2)+” seconds "
Hope this may help you :slight_smile: