473,395 Members | 1,647 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

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

DateTimeFormatInfo dtfi =
(DateTimeFormatInfo)DateTimeFormatInfo.InvariantCu lture.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
Jun 14 '07 #1
3 21180
On Thu, 14 Jun 2007 12:51:24 -0700, Peter Duniho
<Np*********@nnowslpianmk.comwrote:
[...]
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
Jun 19 '07 #2
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

Jun 19 '07 #3
On Tue, 19 Jun 2007 00:33:34 -0700, Jon Skeet [C# MVP] <sk***@pobox.com>
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
Jun 19 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

15
by: Dan S | last post by:
My application asks the user to enter in a date - in the mm/dd/yyyy format. Is there any quick and easy way to verify the date the user enters is formatted correctly? Right now I'm calling...
16
by: PK9 | last post by:
I have a string variable that holds the equivalent of a DateTime value. I pulled this datetime from the database and I want to strip off the time portion before displaying to the user. I am...
0
by: Fred Strauss | last post by:
I am having an odd problem using the DateTime.ParseExact() method that I'm hoping someone can help me with. I have XML files that use 2 different date formats internally. I am trying to use the...
3
by: Dave P. | last post by:
I'm trying to parse a string into a DateTime object. I want to try using ParseExact to see if I can improve performance of a frequently-called method (i.e., please don't suggest using Parse()). ...
4
by: Hans Merkl | last post by:
Does anybody know of a library that can handle strings pf various formats and conver them to a DateTime value? The strings are coming from a webform and I can't restrict the input (it's not my...
2
by: Peter Kirk | last post by:
Hi there I would like some help with parsing date strings to DateTime structures. I can see that DateTime has Parse and ParseExact methods - but I am not sure what is best for me to use, and...
38
by: nobody | last post by:
I know that given a FormatString and a DateTime you can use DateTime.ToString(...) to convert the DateTime to a String. My question is how can you turn that around? Given a String and a...
26
by: Reny J Joseph Thuthikattu | last post by:
Hi, I have a variabe in the format of 'DD-MON-YYYY HH:MI AM' .I want to add a miniute to it.How can i do that? by manipulation i want to make '01-JUNE-2004 11:59 PM' to '02-JUNE-2004 12:00 AM'...
11
by: Cor Ligthert | last post by:
Hello everybody, Jay and Herfried are telling me every time when I use CDate that using the datetime.parseexact is always the best way to do String to datetime conversions. They don't tell why...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.