开发者

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 1开发者_运维技巧9/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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜