Can anyone explain why the following snippet returns true?
According to the docs for The "d" custom format specifier, "A single-digit day is formatted without a leading zero." So why doesn't TryParseExact fail when I give it a single-digit day with a leading zero?
DateTime x;
return DateTime.TryParseExact
(
"01/01/2001",
@"d\/MM\/yyyy",
null,
System.Globalization.DateTimeStyles.None,
out x
);
UPDATE
I think maybe I was unclear originally. What I am really trying to get at is: Why does TryParseExact accept some values that don't match exactly? from all of the documentation I have seen, 'd' matching '01' and '1' is just as much a bug as if 'MM' matched 'March' as well as '03'. The issue here isn't that the values are equivalent, its that they don't match the format.
The relevant snippets of documentation are:
From TryParseExact: The format of the string representation must match a specified format exactly.
From The 'd' Specifier: A single-digit day is formatted without a leading zero.
It seems abundantly clear to me that '01' has a leading 0, and therefore doesn't exactly match 'd'.
From the .NET 4 source in DateTimeParse.ParseByFormat():
The parser lumps "d" and "dd" together.
It appears that behavior is by design, and I think it works that way to be consistent with other string formatting options.
Take the following example:
In the first part,
ToString()
outputs a two digit day for October 12th, because it wouldn't make much sense to just write out a single digit day (and which digit would it pick, the 1 or the 2?). So since the "d" represents one OR two digit days when converting to a string, it would have to work the same way when converting back toDateTime
. If it didn't, the conversion back toDateTime
inTryParseExact
in my example would fail, and that would definitely not be an expected behavior.I would say that if you really need to match a d/MM/yyyy format exactly, you could probably use a regex to validate the string and then pass it through
Parse
,TryParse
orTryParseExact
(depending on how good your regex is, since it would have to handle leap years, 30/31 days, etc if you wanted to useParse
).TryParseExact
is just trying to be flexible in this case I guess. But the "d" vs "dd" should and would work as advertised when you are converting date to string using a format specifier.I'd say it doesn't fail because
TryParseExact
is smart enough to know that '01' == '1'.Because a single 'd' means that your DateTime value will be converted to as short value as possible, i.e. without leading zero if there's no necessity for it. I suppose it shouldn't fail when you're converting from string to DateTime because the main purpose of TryParseExact's format string is to help to convert to DateTime, i.e. it serves like a hint, it's not intended to validate string format.
You can use RegEx if you still need hardcore string format validation.