On Sun, 31 Aug 2008 22:01:56 -0700, John B <jb******@yahoo.comwrote:
Hi all,
Any idea why this code results in a FormatException?
DateTime.ParseExact("40708", "dMMyy", CultureInfo.CurrentCulture)
If I use "040708" with the same format string it works and it parses all
double digit days fine.
I don't know for sure. But it doesn't surprise me that much that it
doesn't work. The degree of analysis that would be required for the
parser to successfully figure deal with variable-length parameters is
non-trivial. The parser is probably trying to parse "40" as a day value
and of course failing.
Your specific example is simpler, but if it's to work, then the parser
would be required to handle all of the variable-length fields as well.
Suppose your format was "dMyy". How does the parser know the difference
between a valid string "41208" (where the day is "4" and the month is
"12") and an invalid string "41208" (where the day is "41" and the month
is "2")? You can't allow the parser to be lenient, because then bad data
could wind up parsed successfully without any indication of an error.
I think the lesson is that if you want to use ParseExact(), your format
string needs to be unambiguous about each character position, which means
the variable-length fields like "d" and "M" are just not a good idea.
Pete