Following code gives me error as shown in title above:
TimeSpan my_hours = new TimeSpan();
my_hours = TimeSpan.Parse("00:00");
my_hours += TimeSpan.Parse("25:07"); //this line throws error
Just before the last line runs value of my_hours is 4.01:33:00. How do I fix this error?
Basically this code is running in a for loop and the value "25:07" keeps changing and it adds in my_hours and it keeps doing it until it tries to add this value "25:07" when current value of my_hours is 4.01:33:00 and throws error.
Change the third line to my_hours += TimeSpan.Parse("00:25:07")
You can read about the expected format of TimeSpan.Parse()
on MSDN:
The s parameter contains a time interval specification in the form:
[ws][-]{ d | [d.]hh:mm[:ss[.ff]] }[ws]
So the bare minimum that is required is hh:mm
. When you put in 25:07
, it was interpreted as 25 hours, seven minutes, which is an illegal value (since hours need to be between 0-23).
Adding in 00:
in front changes it to 0 hours, 25 minutes and 7 seconds, which is now a legal value to parse.
If you want to use more than 24 hours, you have to use a different format. The format that Parse
accepts is documented
[ws][-]{ d | [d.]hh:mm[:ss[.ff]] }[ws]
So you have to specify 1.01:07
for 1 day, 1 hour and 7 minutes.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With