CSharp/C# >> DateTime.ParseExact format issue

by John B » Mon, 01 Sep 2008 13:01:56 GMT

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.

TIA

JB

-- Posted on news://freenews.netfront.net - Complaints to XXXX@XXXXX.COM --

CSharp/C# >> DateTime.ParseExact format issue

by Peter Duniho » Mon, 01 Sep 2008 13:26:08 GMT



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

CSharp/C# >> DateTime.ParseExact format issue

by John B » Mon, 01 Sep 2008 13:32:42 GMT


Thanks for your thoughts Peter, you're correct in a way.
The problem is though that it's data taken out of MS Excel and therefore
any leading zero is stripped.

Cheers,

JB

CSharp/C# >> DateTime.ParseExact format issue

by Peter Duniho » Mon, 01 Sep 2008 13:40:31 GMT


The fact that the data comes from Excel doesn't change anything about what
I wrote. I'm not aware of any requirement that the .NET Framework provide
for built-in processing of data emitted by Excel.

You may well have to do some pre-processing of the data before parsing,
such as adding a leading '0' when the string length is less then 6.
Alternatively, depending on how much control you have over the Excel
worksheet that's providing the data, have Excel emit data that is better
suited to .NET's built-in parsing capabilities.

Pete

CSharp/C# >> DateTime.ParseExact format issue

by John B » Mon, 01 Sep 2008 13:48:23 GMT


Thanks for your response.

JB

CSharp/C# >> DateTime.ParseExact format issue

by QXJuZSBWYWpow7hq » Sun, 07 Sep 2008 08:15:40 GMT


It should be rather simple. Based on the description it is only
the first zero that disappears so replacing:

DateTime.ParseExact(s, "ddMMyy", CultureInfo.CurrentCulture)

with:

DateTime.ParseExact(s.PadLeft(6, '0'), "ddMMyy", CultureInfo.CurrentCulture)

should fix it.

Arne

Similar Threads

1. 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.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

2. DateTime.ParseExact custom format parsing

3. Another DateTime.ParseExact issue?

4. DateTime and ParseExact - CSharp/C#

5. DateTime.ParseExact( ) Exception

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()).  My code looks like this: 

IFormatProvider fp = new System.Globalization.CultureInfo
("en-US"); 
DateTime dt = DateTime.ParseExact("Mar 9, 2005", "MMM d, 
yyyy", fp); 

This throws an exception in MatchAbbreviatedMonthName() 
(an FCL method).   If I look at the CultureInfo object, 
there is, in fact, a month called "Mar". 

How can I use my custom format with ParseExact?

Thanks! 

Dave P.

6. DateTime.ParseExact problem with parsing "8/09/05 9:27a" - CSharp/C#

7. Unexpected result using DateTime.ParseExact

Hi,

I've the following line of code:

result = DateTime.ParseExact("1999-12-01T23:59:59Z", "yyyy-MM-ddTHH:mm:ssZ",
CultureInfo.InvariantCulture);

where I get in result "result"  "02.12.1999 00:59:59" instead of teh 
expected "01.12.1999 23:59:59"
Anybody a idea ?

Thanx   Peter

8. DateTime AddMinutes Format Issue - CSharp/C#