none
tryparseexact "hh:mmtt ddd dd MMM" fails at year end RRS feed

  • Question

  • Generally I have no problems with using the format "hh:mmtt ddd dd MMM", however, since we are at the end of the year, tryparseexact is failing for strings with the format "01:30AM Tue 01 Jan" whereas "01:30AM Tue 31 Dec" is ok (System clock Dec 31, 2012)

    If I change the system clock to Jan 1, 2013, then "01:30AM Tue 01 Jan" parses ok, but "01:30AM Tue 31 Dec" does not.

    I am scraping data from the web, so I have no direct control over the input format.  Other than testing what the currrent day is, and then parsing the month to decide if I am near the end of the year and then add the year to the input date string,  are there any simpler suggestions to get around this issue I am facing?

    Sunday, December 30, 2012 10:57 PM

Answers

  • Hi Ray,

    Welcome to the MSDN Forum.

    I am afraid that the date is a wrong date, because Dec 31, 2012 is Monday, not Tuesday. I tried to change it to Mon 

            Dim dt As DateTime = DateTime.ParseExact("01:30AM Mon 31 Dec", "hh:mmtt ddd dd MMM", Nothing)
            Console.WriteLine(dt)

    And it works as expected.

    So when you cannot convert it, it means you got a wrong date time. About how to handle this issue, it is not the original issue in this thread.

    Thank you.

    Best regards,


    Mike Feng
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Monday, December 31, 2012 5:33 AM
    Moderator

All replies

  • That is because, since you have not specified any year, by default it assumes the current year i.e. 2012. But 01-Jan-2012 was not Tuesday (it was Sunday). Hence it will throw you the exception. However, if you add the year to your input, it will work.

    string format = "hh:mmtt ddd dd MMM yy";
    string input = "01:30AM Tue 01 Jan 13";
    
    DateTime dt = DateTime.ParseExact(input, format, null);

    I hope this helps.

    Please mark this post as answer if it solved your problem. Happy Programming!

    Monday, December 31, 2012 5:17 AM
  • Hi Ray,

    Welcome to the MSDN Forum.

    I am afraid that the date is a wrong date, because Dec 31, 2012 is Monday, not Tuesday. I tried to change it to Mon 

            Dim dt As DateTime = DateTime.ParseExact("01:30AM Mon 31 Dec", "hh:mmtt ddd dd MMM", Nothing)
            Console.WriteLine(dt)

    And it works as expected.

    So when you cannot convert it, it means you got a wrong date time. About how to handle this issue, it is not the original issue in this thread.

    Thank you.

    Best regards,


    Mike Feng
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Monday, December 31, 2012 5:33 AM
    Moderator