C# – DateTime.Parse issue, not support in System.Globalization.GregorianCalendar

cdatetimenet

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.

Best Answer

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;
}