DateTime.ParseExact返回FormatExpcetion异常

4
我有一个奇怪的问题:
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"));

返回 FormatException = "字符串无法被识别为有效的日期时间。"

但是该代码可以不出现异常:

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"));

从按照该格式解析的30k个数据中,约有50%出现了该异常...

有人知道为什么吗?

2个回答

10

应该使用HH而不是hh。你处于24小时制。

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

有效的:Sat Apr 10 01:27:00 GMT-0500 2010


2

看起来DateTime在"en"格式提供程序中需要AM/PM信息。尝试使用小于12(包括12)的任何小时,或添加一些AM/PM信息。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接