DateTime.Parse() vs DateTime.ParseExact()

P

Peter Duniho

I'm sure there's a good explanation for this, but I can't figure it out.

I tried using DateTime.Parse() with a custom DateTimeFormatInfo instance,
in which I'd replaced the DateTimeFormatInfo.FullDateTimePattern property
with my custom format string:

DateTimeFormatInfo dtfi =
(DateTimeFormatInfo)DateTimeFormatInfo.InvariantCulture.Clone();

dtfi.FullDateTimePattern = "dd/MMM/yyyy:HH:mm:ss zzz";

DateTime dt = DateTime.Parse("23/Mar/2007:13:22:28 -0600", dtfi,
DateTimeStyles.AdjustToUniversal);

For some reason, that doesn't work. If I try the exact same format string
with DateTime.ParseExact(), it works fine.

My expectation was that the Parse() method would try all of the various
format strings it knows about, which would include the FullDateTimePattern
string I set. But apparently it doesn't do that.

Can anyone tell me what it _does_ do, and why it doesn't at least include
all of the patterns set within the format pattern properties given to it?

In my case, using ParseExact() is a reasonable work-around, but I'm
wondering if there's a way to do this using the Parse() method. It sure
seems like it ought to work.

Thanks,
Pete
 
P

Peter Duniho

[...]
My expectation was that the Parse() method would try all of the various
format strings it knows about, which would include the
FullDateTimePattern string I set. But apparently it doesn't do that.

Can anyone tell me what it _does_ do, and why it doesn't at least
include all of the patterns set within the format pattern properties
given to it?

Really? No one here knows enough about how DateTime.Parse() works to
explain why it doesn't match my input string to the format I've provided?

Or did my post just go unnoticed somehow?

Pete
 
J

Jon Skeet [C# MVP]

Really? No one here knows enough about how DateTime.Parse() works to
explain why it doesn't match my input string to the format I've provided?

Or did my post just go unnoticed somehow?

It was certainly unnoticed by me.

I can't say I can really explain it, although the docs have a *hint*
about it:

<quote>
If you parse a date and time string generated for a custom culture,
use the ParseExact method instead of the Parse method to improve the
probability that the parse operation will succeed. A custom culture
date and time string can be complicated, and therefore difficult to
parse. The Parse method attempts to parse a string with several
implicit parse patterns, all of which might fail.
</quote>

Now, it's not clear what those "several implicit parse patterns" are,
but it sounds like it's not looking at the *patterns* from the format
provider you specify, just other bits (like the month names etc).

Sorry it's not a more helpful answer...

Jon
 
P

Peter Duniho

[...]
I can't say I can really explain it, although the docs have a *hint*
about it:

<quote>
If you parse a date and time string generated for a custom culture,
use the ParseExact method instead of the Parse method to improve the
probability that the parse operation will succeed. A custom culture
date and time string can be complicated, and therefore difficult to
parse. The Parse method attempts to parse a string with several
implicit parse patterns, all of which might fail.
</quote>

Yeah, I saw that. It's in fact why I wound up getting to the ParseExact()
method so quickly; otherwise, I might have fumbled around longer than I
did. :)
Now, it's not clear what those "several implicit parse patterns" are,
but it sounds like it's not looking at the *patterns* from the format
provider you specify, just other bits (like the month names etc).

I agree. :) But I was left wondering, if it's not looking at the
patterns from the format provider, what is it looking at, and why do I
bother providing a format provider if it's not going to look at the
patterns in the format provider? Or maybe the question should be directed
the other way: why doesn't the format provider use its own patterns in
providing parsing help to the Parse() method (since it don't really
understand how the format provider works, I don't know which way the
question should be worded).
Sorry it's not a more helpful answer...

Well, at least it helps me feel better about not understanding it myself.
:)

Thanks,
Pete
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top