locked
DateTime ISO 8601

    Question

  • Hi All, 

    Question about DateTime object in C#,

    In MSDN it says, one of the string that can be parsed "A string that includes time zone information and conforms to ISO 8601".

    But, If you refer to ISO 8601 about the midnight case: "Midnight is a special case and can be referred to as both "00:00" and "24:00" (wikipedia)

     

    The problem is, The datetime parser cannot parse the 24:00.

     

    For example I've a string: "2006-06-06T24:00:00.000Z",

    it will throw exception: "The DateTime represented by the string is not supported in calendar System.Globalization.GregorianCalendar."

     

    Do you have any idea?

     

    Note: I've tried an alternative which is adding a condition to to Check & Replace T24 to T00 everytime before parsing, but its decrease my application performance.



    Regards, Christofel
    Thursday, August 25, 2011 3:07 AM

Answers

  • You get an error, when you try to parse this string to dateTime, right?

    If there is an error, why dont you check the time (hh:mm:sss) it they are as "00:00:000Z", and change it to "24:00:000Z"!?


    Mitja
    Thursday, August 25, 2011 3:27 AM
  • You can't just replace 24:00 with 00:00. If you do that, you must also increment the date by 1 day, since 24:00 indicates midnight at the end of the day.
    Thursday, August 25, 2011 7:56 AM
  • It is a .NET bug definitely so your solution is correct.

    But you could improve it by performing the replace only if conversion is failed. In this case performance will be affected only for problematic dates, not for others.

    Something like:

     

    DateTime dt;
    if (!DateTime.TryParse(strDate, out dt))
    {
     strDate = strDate.Replace(...) // your fix here
     dt = DateTime.Parse(strDate);
    }
    


     

     


    Thursday, August 25, 2011 12:23 PM
  • Hi Christofel,
    Have you read VM-Pire's suggestion? I think it is constructive. Below is maybe a useful improvement:
    DateTime dt;
    if (!DateTime.TryParse(strDate, out dt))
    {
        //Your logic to replace, maybe it is not in this way.
        strDate = strDate.Replace(...);
    
        if (!DateTime.TryParse(strDate, out dt))
        {
           Console.WriteLine("Your date string is not in the right format, please recheck it!");
        }
    
        else
           dt.AddDays(1); //Add one day as Matthew suggested.
    }
    

    Please let us know if any progress is made.

    Have a nice day,

    Leo Liu [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Monday, August 29, 2011 5:15 AM
    Moderator

All replies

  • You get an error, when you try to parse this string to dateTime, right?

    If there is an error, why dont you check the time (hh:mm:sss) it they are as "00:00:000Z", and change it to "24:00:000Z"!?


    Mitja
    Thursday, August 25, 2011 3:27 AM
  • Thanks for your response.

    Yes. I got the error when trying to parse.

    I've tried to change it, it works but slowdown my aplication performance since i've to add condition to check & replace String before parsing.

    Any alternative rather than check and replacing string?


    Regards, Christofel
    Thursday, August 25, 2011 3:47 AM
  • On first look it appears to be the problem with .NET. May be T24:00 format is not incorporated in calendar.

    So, may be you need to replace 24 as 00.


    Please mark this post as answer if it solved your problem. Happy Programming!
    Thursday, August 25, 2011 6:14 AM
  • You can't just replace 24:00 with 00:00. If you do that, you must also increment the date by 1 day, since 24:00 indicates midnight at the end of the day.
    Thursday, August 25, 2011 7:56 AM
  • Thanks for your reply.

    Yes of course, while replace the T24 to T00 need to add 1 of the current day.

    That is the solution that i applied right now. But i try to find other alternative, since checking and replacing string slowdown my application.

    Any other suggestion rather than replacing the string?

    Does the 24:00 format doesn't support by .Net at all?

     


    Regards, Christofel
    Thursday, August 25, 2011 8:24 AM
  • Hi,

    The only thing left I see would be to see if parsing the date yourself (or splitting to the string to have .NET parsing separately each part one as a date, the other as a timespan and adding them) is quicker than altering the string and have it parsed by .NET...

    I'm a bit surprised that it makes such a difference but I assume you are processing tons of dates ? Is the decrease really that significant ?


    Please always mark whatever response solved your issue so that the thread is properly marked as "Answered".

    Thursday, August 25, 2011 9:25 AM
  • From looking at several threads about this on StackOverflow, it seems not. You have already implemented the best solution.
    Thursday, August 25, 2011 9:40 AM
  • Hi Patrice,

    Thanks for your response.

    Yes i'm thinking to create the custom parser, if there was no alternative using built-in object from .net.

    The applications processing lots of date time and needs to be real-time.


    Regards, Christofel
    Thursday, August 25, 2011 10:15 AM
  • Hi Mattew,

    Yes i've been explored the similiar threads on thats forum also. All solution refer to this.

     


    Regards, Christofel
    Thursday, August 25, 2011 10:16 AM
  • It is a .NET bug definitely so your solution is correct.

    But you could improve it by performing the replace only if conversion is failed. In this case performance will be affected only for problematic dates, not for others.

    Something like:

     

    DateTime dt;
    if (!DateTime.TryParse(strDate, out dt))
    {
     strDate = strDate.Replace(...) // your fix here
     dt = DateTime.Parse(strDate);
    }
    


     

     


    Thursday, August 25, 2011 12:23 PM
  • It has already been discussed in a connect issue, over 6 years ago. It seems it is set to closed/won't fix at the moment.

    http://connect.microsoft.com/VisualStudio/feedback/details/119595/it-appears-that-24-00-00-is-treated-as-an-invalid-time

    Go there and vote for it, comment on it and see if you can get it reconsidered if you want this fixed.

     

    Thursday, August 25, 2011 12:51 PM
  • Hi Christofel,
    Have you read VM-Pire's suggestion? I think it is constructive. Below is maybe a useful improvement:
    DateTime dt;
    if (!DateTime.TryParse(strDate, out dt))
    {
        //Your logic to replace, maybe it is not in this way.
        strDate = strDate.Replace(...);
    
        if (!DateTime.TryParse(strDate, out dt))
        {
           Console.WriteLine("Your date string is not in the right format, please recheck it!");
        }
    
        else
           dt.AddDays(1); //Add one day as Matthew suggested.
    }
    

    Please let us know if any progress is made.

    Have a nice day,

    Leo Liu [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Monday, August 29, 2011 5:15 AM
    Moderator
  • Hi Thanks All for you feedback.

    Yes i think i should use the TryParse method.


    Regards, Christofel
    Wednesday, September 07, 2011 9:06 AM