【问题标题】:Parsing DateTime HHmmss解析日期时间 HHmmss
【发布时间】:2013-02-04 21:07:00
【问题描述】:

最近我试图解析这个时间 24:14:38 必须是 12:14:38AM

到目前为止,这是我的功能

private static DateTime ParseDate(string time)
{
    DateTime result;
    if (!DateTime.TryParse(time, out result))
    {
        result = DateTime.ParseExact(time, "HHmmss", System.Globalization.CultureInfo.InvariantCulture);
    }
    return result;
}

这是如何使用的

    var time = "24:14:38";
    var result = ParseDate(time.Replace(":", ""));
    Console.WriteLine(result);

但是我一直收到这个错误

日历不支持字符串表示的DateTime System.Globalization.GregorianCalendar。

已解决 24 在 24 小时制中无效,我应该使用 00:14:38

【问题讨论】:

    标签: c# winforms parsing datetime


    【解决方案1】:

    简单。以“24”作为小时值的时间字符串不是有效的时间字符串。

    12 小时制有效时间的小时值是 1-12,24 小时制是 0-23。

    【讨论】:

    • 那么 12:14:38 怎么能用 HHmmss 呈现呢!
    • 12:14:38 AM 将是 00:14:38
    猜你喜欢
    • 2014-01-29
    • 2011-01-23
    • 1970-01-01
    • 1970-01-01
    • 2015-07-22
    • 2020-09-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多