DateTime.Parse() vs DateTime.ParseExact()

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Peter Duniho

    DateTime.Parse() vs DateTime.ParseExact()

    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 DateTimeFormatI nfo instance,
    in which I'd replaced the DateTimeFormatI nfo.FullDateTim ePattern property
    with my custom format string:

    DateTimeFormatI nfo dtfi =
    (DateTimeFormat Info)DateTimeFo rmatInfo.Invari antCulture.Clon e();

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

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

    For some reason, that doesn't work. If I try the exact same format string
    with DateTime.ParseE xact(), 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 FullDateTimePat tern
    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
  • Peter Duniho

    #2
    Re: DateTime.Parse( ) vs DateTime.ParseE xact()

    On Thu, 14 Jun 2007 12:51:24 -0700, Peter Duniho
    <NpOeStPeAdM@nn owslpianmk.comw rote:
    [...]
    My expectation was that the Parse() method would try all of the various
    format strings it knows about, which would include the
    FullDateTimePat tern 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

    Comment

    • Jon Skeet [C# MVP]

      #3
      Re: DateTime.Parse( ) vs DateTime.ParseE xact()

      On Jun 19, 4:28 am, "Peter Duniho" <NpOeStPe...@nn owslpianmk.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

      Comment

      • Peter Duniho

        #4
        Re: DateTime.Parse( ) vs DateTime.ParseE xact()

        On Tue, 19 Jun 2007 00:33:34 -0700, Jon Skeet [C# MVP] <skeet@pobox.co m>
        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

        Comment

        Working...