Until now i thought that i would understand how DateTime.ParseExact
works, but this is confusing. Why does following line returns false
?
DateTime.TryParseExact("2013122", "yyyyMdd", CultureInfo.InvariantCulture, System.Globalization.DateTimeStyles.None, out lastUpdate)
The month can also have two digits. In my opinion it should be able to understand that it means 22 January 2013. Why i'm on the wrong track? Did I miss something or is there an easy workaround?
Meanwhile i'm using this workaround which is not very elegant but works:
public static DateTime? ParseDate_yyyyMdd(String date)
{
if (date == null)
return null;
date = date.Trim();
if (date.Length < 7)
return null;
if (date.Length == 7)
date = date.Insert(4, "0");
DateTime dt;
if (DateTime.TryParseExact(date, "yyyyMMdd", CultureInfo.InvariantCulture, System.Globalization.DateTimeStyles.None, out dt))
return dt;
return null;
}
Gives my desired result:
DateTime? date = ParseDate_yyyyMdd("2013122");
Console.Write(date.ToString()); // 01/22/2013
However, i'm still interested in the reason for this limitation. Maybe someone also has a better approach.
http://msdn.microsoft.com/en-us/library/ms131044(v=vs.110).aspx
I did track down that in source code. Which confirms the answers of flipchart and Mark Sturgill.
Somewhere an internal ParseByFormat is called which counts (in your case) the 'M':
The next call is not very interesting, except for the 2 little numbers in ParseDigits call:
But now we get to the fun part:
So that means (as already answered):
If you do not use the maximum number of digits AND the next character is also a digit, the format is not valid. Which is the reason why the following returns true:
Don't ask me about the reasons for that limitation - it is just there in source code.
From MSDN documentation:
I think that the reason is that it tries to parse left to right (without backtracking). Because there are no delimiters, it can't determine the boundaries of the date parts.