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.
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.
My suspicion is that it's because of the hour 24... rather than 00, but I'm not sure how I would rectify that.
thank you. "The DateTime represented by the string is not supported in calendar System.Globalization.GregorianCalendar." means that the string value that you are passing an invalid datetime string to the Date.Parse () method. The only way that I could reproduce that specific error message is with an invalid time component.
When working with historic dates that precede a culture's adoption of the Gregorian calendar, you should use the original calendar if it is available in the .NET Framework. For example, Denmark changed from the Julian calendar to the Gregorian calendar on February 19 (in the Julian calendar) or March 1 (in the Gregorian calendar) of 1700.
The Gregorian calendar recognizes two eras: B.C. or B.C.E., and A.D. or C.E. This implementation of the GregorianCalendar class recognizes only the current era (A.D. or C.E.). For information about using the GregorianCalendar class and the other calendar classes in the .NET Framework, see Working with Calendars.
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;
}
If it IS just the 24
instead of 00
, you can simply replace it and add a day:
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);
}
The DateTime entry in MSDN says that it supports ISO 8601 which allows both 24 and 00. It should allow the format of type [YYYY][MM][DD]T[hh][mm]Z
eg. 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).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With