Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is wrong with this TimeSpan.ParseExact line?

I am receiving a FormatError exception from this call to TimeSpan.ParseExact, but the MSDN documentation that I am reading says that this format should be correct:

TimeSpan timeSpan = TimeSpan.ParseExact("172100", "HHmmss", CultureInfo.InvariantCulture);

Can someone please tell me why this is not working? I am doing almost exactly the same thing with a call to DateTime.ParseExact and this works fine:

DateTime datetTime = DateTime.ParseExact("090820", "yyMMdd", CultureInfo.InvariantCulture);
like image 203
Steve Rukuts Avatar asked Apr 06 '11 15:04

Steve Rukuts


2 Answers

TimeSpan does not use the same formatting rules as DateTime.

You want hhmmss, not HHmmss.

You're looking at the wrong page in MSDN - you want something like:

http://msdn.microsoft.com/en-us/library/se73z7b9.aspx

like image 57
Will Dean Avatar answered Oct 09 '22 12:10

Will Dean


With ref to this more accurate documentation: http://msdn.microsoft.com/en-us/library/ee372287.aspx

You need to use hh for hours, not HH.

like image 28
Kieren Johnstone Avatar answered Oct 09 '22 11:10

Kieren Johnstone