PC Review


Reply
Thread Tools Rate Thread

DateTime.Parse() vs DateTime.ParseExact()

 
 
Peter Duniho
Guest
Posts: n/a
 
      14th Jun 2007
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
 
Reply With Quote
 
 
 
 
Peter Duniho
Guest
Posts: n/a
 
      19th Jun 2007
On Thu, 14 Jun 2007 12:51:24 -0700, Peter Duniho
<(E-Mail Removed)> wrote:

> [...]
> 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
 
Reply With Quote
 
Jon Skeet [C# MVP]
Guest
Posts: n/a
 
      19th Jun 2007
On Jun 19, 4:28 am, "Peter Duniho" <NpOeStPe...@nnowslpianmk.com>
wrote:
> > 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?


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

 
Reply With Quote
 
Peter Duniho
Guest
Posts: n/a
 
      19th Jun 2007
On Tue, 19 Jun 2007 00:33:34 -0700, Jon Skeet [C# MVP] <(E-Mail Removed)>
wrote:

> [...]
> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
DateTime and ParseExact =?Utf-8?B?c2lwcHl1Y29ubg==?= Microsoft C# .NET 2 12th Apr 2007 03:58 AM
DateTime.ParseExact(..) Pablo Microsoft VB .NET 1 16th Aug 2006 01:25 PM
Another DateTime.ParseExact issue? =?Utf-8?B?SmFjZWsgS29sb25rbw==?= Microsoft Dot NET Framework 1 19th May 2006 06:37 AM
DateTime.ParseExact( ) usage Dave P Microsoft Dot NET Framework 1 23rd Mar 2005 12:13 AM
DateTime.ParseExact( ) Exception Dave P. Microsoft C# .NET 3 15th Mar 2005 03:39 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 02:57 AM.