C# DateTime.Parse 问题,System.Globalization.GregorianCalendar 不支持

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2000343/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 22:32:42  来源:igfitidea点击:

DateTime.Parse issue, not support in System.Globalization.GregorianCalendar

c#.netdatetime

提问by Alastair Pitts

I'm having an issue where a specific time string, contained in the Gmail Atom feed isn't parsing using DateTime.Parse(). I understand I could use DateTime.TryParse(), but I'm curious to why these two don't work, where as all of the rest do.

我遇到了一个问题,即 Gmail Atom 提要中包含的特定时间字符串未使用DateTime.Parse(). 我知道我可以使用 DateTime.TryParse(),但我很好奇为什么这两个不起作用,而其余的都不起作用。

2009-12-28T24:11:48Z
2009-12-30T24:16:20Z

the specific exception is:

具体的例外是:

System.FormatException: The DateTime represented by the string is not supported in calendar System.Globalization.GregorianCalendar.

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

My suspicion is that it's because of the hour 24... rather than 00, but I'm not sure how I would rectify that.

我怀疑是因为 24 小时......而不是 00,但我不确定我将如何纠正它。

采纳答案by Martin Brown

private static DateTime ParseDate(string s)
{
    DateTime result;
    if (!DateTime.TryParse(s, out result))
    {                
        result = DateTime.ParseExact(s, "yyyy-MM-ddT24:mm:ssK", System.Globalization.CultureInfo.InvariantCulture);
        result = result.AddDays(1);
    }
    return result;
}

回答by Oren Mazor

turns out google's using the w3c date and time standard: http://www.w3.org/TR/NOTE-datetime, which c# doesnt support? weird, but not that weird.

原来谷歌使用 w3c 日期和时间标准:http: //www.w3.org/TR/NOTE-datetime,哪个 c# 不支持?奇怪,但没那么奇怪。

this project appears to implement that for you.

这个项目似乎为你实现了。

http://www.codeproject.com/KB/cs/w3cdate.aspx

http://www.codeproject.com/KB/cs/w3cdate.aspx

edit: yes, I see now that google's not doing it right, but there's an exception for that in the w3cdate c# structure.

编辑:是的,我现在看到谷歌做得不对,但在 w3cdate c# 结构中有一个例外。

回答by Nathan Wheeler

If it IS just the 24instead of 00, you can simply replace it and add a day:

如果它只是24代替00,您可以简单地替换它并添加一天:

String s = "2009-12-28T24:11:48Z";
DateTime dt;
if (s.Contains("T24:")
{
    s = s.Replace("T24:", "T00:");

    if (DateTime.TryParse(s, out dt))
        dt.AddDays(1);
}
else
{
    DateTime.TryParse(s, out dt);
}

回答by James Bloomer

The DateTime entry in MSDNsays that it supports ISO 8601which allows both 24 and 00. It should allow the format of type [YYYY][MM][DD]T[hh][mm]Zeg. 2010-01-04T14:04Z.

MSDN 中的 DateTime 条目说它支持ISO 8601,它允许 24 和 00。它应该允许类型的格式,[YYYY][MM][DD]T[hh][mm]Z例如。2010-01-04T14:04Z.

Midnight is a special case and can be referred to as both "00:00" and "24:00". The notation "00:00" is used at the beginning of a calendar day and is the more frequently used. At the end of a day use "24:00". Note that "2007-04-05T24:00" is the same instant as "2007-04-06T00:00" (see Combined date and time representations below).

午夜是一种特殊情况,可以称为“00:00”和“24:00”。符号“00:00”用于日历日的开始,使用频率更高。在一天结束时使用“24:00”。请注意,“2007-04-05T24:00”与“2007-04-06T00:00”是同一时刻(请参阅下面的组合日期和时间表示)。

回答by Benjamin Podszun

Same idea as md5sum, but a different way:

与 md5sum 相同的想法,但方式不同:

 DateTime.ParseExact(
  "2009-12-28T24:11:48Z",
  new []{ "yyyy-MM-ddTHH:mm:ssK", "yyyy-MM-ddT24:mm:ssK" }, 
  System.Globalization.CultureInfo.InvariantCulture,
  System.Globalization.DateTimeStyles.None
)

You'd still need to check if the date is correct though.

不过,您仍然需要检查日期是否正确。