ISO 8601值24:00:00的解析日期失败 [英] Parse date of ISO 8601 value 24:00:00 fails
问题描述
我正在尝试从数据源中解析传入日期(无法更改)。它给我的时间在ISO 8601格式的例子: 2007-04-05T24:00
。
I'm trying to parse incoming date from data source (which cannot be changed). It gives me time in ISO 8601 format example: 2007-04-05T24:00
.
Net无法将其解析为有效时间。
How ever in .Net it fails to parse this as valid time.
维基百科表示它应该是有效的格式。 维基百科ISO 8601
The wikipedia states that it should be valid format. Wikipedia ISO 8601
a href =http://stackoverflow.com/a/3556188/645410> http://stackoverflow.com/a/3556188/645410
Example from http://stackoverflow.com/a/3556188/645410
如果没有一个讨厌的字符串检查黑客,我该怎么做?
How can I do this without a nasty string check hack?
示例(fiddle: http://dotnetfiddle.net/oB7EZx ):
Example (fiddle: http://dotnetfiddle.net/oB7EZx):
var strDate = "2007-04-05T24:00";
Console.WriteLine(DateTime.Parse(strDate, null, DateTimeStyles.RoundtripKind));
投掷:
日历
System.Globalization.GregorianCalendar中不支持由字符串表示的DateTime。
The DateTime represented by the string is not supported in calendar System.Globalization.GregorianCalendar.
推荐答案
是的,.NET根本就不支持这一点。
Yes, .NET doesn't support this as far as I'm aware.
我的 Noda Time 项目,但只是部分:它可以解析的价值,但价值只是在第二天开始解析为午夜,而且永远不会格式为24:00。 Noda Time概念模型中没有什么可以表示一天结束。
My Noda Time project does, but only partially: it can parse the value, but the value is just parsed to midnight at the start of the next day, and is never formatted as 24:00. There's nothing in the Noda Time conceptual model to represent "the end of the day".
示例代码以显示可能的内容:
Sample code to show what's possible:
using System;
using NodaTime;
using NodaTime.Text;
class Test
{
static void Main()
{
string text = "2007-04-05T24:00";
var pattern = LocalDateTimePattern.CreateWithInvariantCulture
("yyyy-MM-dd'T'HH:mm");
var dateTime = pattern.Parse(text).Value;
Console.WriteLine(pattern.Format(dateTime)); // 2007-04-06T00:00
}
}
如果你不介意丢失2007-04-05T24:00和2007-04-05T00:00的输入之间的差异,那么你可能还好。
If you don't mind losing the difference between inputs of "2007-04-05T24:00" and "2007-04-05T00:00" then you're probably okay.
这篇关于ISO 8601值24:00:00的解析日期失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!