-1

I'm not sure what's going on here, but it will accept some timespans, but not others. Can someone show me a way to check for a vaild time span in this format 99:59:59.

//50:30:00 is bad
//50:20:00 is good

try
{
    TimeSpan ts = new TimeSpan();
    ts = TimeSpan.Parse("50:30:00");
}
catch //(Exception ex)
{
    MessageBox.Show("bad time span");
}
Rikki B
  • 636
  • 8
  • 23

1 Answers1

6

By default, the Timespan.Parse method assumes that the time is int the format Days:Hours:Minutes. Since you can't have more than 24 hours in a day, it throws when the Hours component is greater than 24.

I have to admit I'm a bit surprised that Timespan.Parse won't do the conversion for you. I have a hunch it has something to do with the fact that not all days are 24 hours long.

Jason Watkins
  • 3,766
  • 1
  • 25
  • 39
  • That doesn't explain why it will accept 48:00:00 or 50:20:00 though. Can you explain that to me? – Rikki B Mar 25 '13 at 04:35
  • 3
    Sure it does. `48:00:00` is read as "48 days, 0 hours, 0 minutes" (legal). `50:20:00` is read as "50 days, 20 hours, 0 minutes" (legal). `50:30:00` is read as "50 days, 30 hours, 0 minutes" (not legal). Days can't have 30 hours in them, so it throws and exception. – Jason Watkins Mar 25 '13 at 04:38
  • Ah thanks for that I understand how it's working now. I couldn't understand it. – Rikki B Mar 25 '13 at 04:39