c# - DateTime.ParseExact 返回 FormatException

标签 c# .net string exception datetime

我有一个奇怪的问题:

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 = “字符串未被识别为有效的 DateTime。”

但是这段代码没有异常(exception):

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% 的解析因该异常而失败...

有人知道为什么吗?

最佳答案

它应该是 HH 而不是 hh。您采用的是 24 小时制。

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

有效时间:2010 年 4 月 10 日星期六 01:27:00 GMT-0500

关于c# - DateTime.ParseExact 返回 FormatException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11674165/

相关文章:

c# - 如何自动隐藏消息框?

c# - 带有 using 子句的 SqlConnection - 在连接上调用关闭

.net - 为什么 roslyn 分析器需要是可移植组件?

.net - 如何计算 2 个或更多不超过特定值的值集之间的可能数字的数量?

java - 从 switch 语句返回 null 的字符串语句

c - 在 C 中标记字符串

c# - 通过 Selenium 检查 SelectElement 元素中是否存在选项

c# - CRM 2011 插件中的线程/环境上下文

基于 C# .NET 文档的应用程序操作图

java - 将字符串拆分为重复的字符