Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c#

.net

datetime

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.

like image 697
Alastair Pitts Avatar asked Jan 04 '10 15:01

Alastair Pitts


People also ask

Why is the datetime string not supported in the calendar?

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.

Should I use the Julian or Gregorian calendar?

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.

What are the different eras of the Gregorian calendar?

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.


3 Answers

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;
}
like image 60
Martin Brown Avatar answered Sep 20 '22 07:09

Martin Brown


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);
}
like image 30
Nathan Wheeler Avatar answered Sep 19 '22 07:09

Nathan Wheeler


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).

like image 28
James Bloomer Avatar answered Sep 20 '22 07:09

James Bloomer