'Parse date of ISO 8601 value 24:00:00 fails

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.

How ever in .Net it fails to parse this as valid time.

The wikipedia states that it should be valid format. Wikipedia ISO 8601

Example from https://stackoverflow.com/a/3556188/645410

How can I do this without a nasty string check hack?

Example (fiddle: http://dotnetfiddle.net/oB7EZx):

var strDate = "2007-04-05T24:00";       
Console.WriteLine(DateTime.Parse(strDate, null, DateTimeStyles.RoundtripKind));

Throws:

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



Solution 1:[1]

Here is one simple solution — it updates this kind of end-of-the-day values to the start-of-the-next-day:

using System;
using System.Text.RegularExpressions;
    
namespace Iso8601ToDateTime
{
  class Program
  {
    string text = "2021-12-31T24:00:00+01:00";
    var pattern = @"^([-\d]+)(T24)";
    var replaced = Regex.Replace(text, pattern,
            m => DateTime.Parse(m.Groups[1].Value)
                    .AddDays(1).ToString("yyyy-MM-dd") + "T00");

    Console.WriteLine(replaced);  // 2022-01-01T00:00:00+01:00
  }
}

UPDATED: Fixed bug based on raznagul's comment.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1