Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DateTime.ParseExact problem with Thai / Buddhist Era Time

After client downloads a file from our server with our app, the app does a ParseExact on a date string which comes down from the server in the form: yyyy/mm/dd HH:mm:ss.

After alot of confusion, I noticed in some logs that the date on the clients system was 19/7/2554. As is turns out this is a valid time as in Thailand, Windows defaults to the Buddhist era time system, where it is the year 2554.

My parse exact is done with an invariant culture, which I suspect may be the problem, but I thought that the culture referred to the format which you were trying to parse?

The exception message I get is: String was not recognized as a valid DateTime because the day of week was incorrect

like image 982
James R Avatar asked Jul 29 '11 04:07

James R


1 Answers

The CultureInfo also contains calendar information. If the TryParseExact method has access to the correct calendar information then it will be able to parse the date correctly.

http://msdn.microsoft.com/en-us/library/system.globalization.cultureinfo.calendar.aspx

If you replace the InvariantCulture CultureInfo with the Thai CultureInfo then the default calendar for that culture will be used.

Alternatively, you could use an overload of the TryParse method that does not require a CultureInfo. The culture that is used in this case will be dependant on the user's regional settings in Windows.

like image 114
Scott Munro Avatar answered Oct 21 '22 02:10

Scott Munro