0
votes

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

2
Why are people down voting? This is a classic gotcha!3-14159265358979323846264

2 Answers

3
votes

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.

2
votes

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