0

I've a problem with DateTime TryParseExact.

I need to parse a string date in this format : "10 Fri, Jun 2013"

My pattern is : "d ddd, MMM yyyy"

Look at the code bellow

 private static readonly string[] EnglishFormats = 
    { 
        "yyyy-MM-ddTHH:mm:sszzz", "dd MMMM yyyy HH:mm" , "dddd, MMMM d, yyyy","dddd, d MMMM yyyy","dddd, MMMM d, yy","dddd, d MMMM yy","d ddd, MMM yyyy"
    };

   public static bool TryParseEnglishDate(string s, out DateTime result)
    {
        return DateTime.TryParseExact(s, EnglishFormats, CultureInfo.InvariantCulture, DateTimeStyles.None, out result);
    }

When s = "10 Fri, Jan 2014" that works ==> result = 1/10/2014 12:00:00 AM

When s = "10 Fri, Jan 2013" or other years it doesn't work ==> result= "1/1/0001 12:00:00 AM"

Have you got an idea why it doesn't work with differents year than 2014 ?

Thanks

1
  • Why are people down voting? This is a classic gotcha! Commented Jul 17, 2015 at 9:44

2 Answers 2

3

The 10th of Jan 2013 isn't a Friday. It parses the date correctly and doesn't selectively ignore sections of the format, so you've basically given it a date it cannot represent.

The format is fine, the string you are trying to parse is not.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for your quick response !!! It was an example. :) But this could be a good answer ! If s = "6 Mon, Jan 2013" it doesn't work too result = "1/1/0001 12:00:00 AM"
Err ... that's a Sunday. Seriously ... get a calendar!
2

10th Jan 2013 is a Thursday. So that input is not a valid date.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.