Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DateTime.ParseExact returning FormatExpcetion

I have a strange problem:

string format = @"ddd MMM dd hh:mm:ss \G\M\Tzzz yyyy";
__timestamp = "Fri Apr 09 17:02:00 GMT-0500 2010";
DateTime.ParseExact(__timestamp, format, new CultureInfo("en"));

returning FormatException = "String was not recognized as a valid DateTime."

but that code going without exceptions:

string format = @"ddd MMM dd hh:mm:ss \G\M\Tzzz yyyy";
__timestamp = "Sat Apr 10 01:27:00 GMT-0500 2010";
DateTime.ParseExact(__timestamp, format, new CultureInfo("en"));

From 30k of date parsing of that format, around 50% of that failed with that exception...

Anyone know why?

like image 799
Svisstack Avatar asked Dec 16 '22 21:12

Svisstack


2 Answers

it should be HH not hh. You're in the 24-hr format.

ddd MMM dd HH:mm:ss \G\M\Tzzz yyyy

Valid: Sat Apr 10 01:27:00 GMT-0500 2010

like image 76
John Woo Avatar answered Jan 01 '23 19:01

John Woo


Seems that DateTime is expecting AM/PM info for that "en" format provider. Try it with any hours less than 12 (inclusive), or add some AM/PM information

like image 22
Egor Avatar answered Jan 01 '23 19:01

Egor