【问题标题】:DateTime.ParseExact returning FormatExpcetionDateTime.ParseExact 返回 FormatException
【发布时间】:2012-07-25 07:50:49
【问题描述】:

我有一个奇怪的问题:

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% 的日期解析失败,但出现该异常...

有人知道为什么吗?

【问题讨论】:

    标签: c# .net string exception datetime


    【解决方案1】:

    应该是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 小时(含)的时间,或添加一些 AM/PM 信息

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2023-03-22
        • 2017-04-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-11-24
        • 2021-11-16
        相关资源
        最近更新 更多