472,951 Members | 2,013 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

DateTime.ParseExact format issue

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 ne**@netfront.net --
Sep 1 '08 #1
5 5510
On Sun, 31 Aug 2008 22:01:56 -0700, John B <jb******@yahoo.comwrote:
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.
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
Sep 1 '08 #2
Peter Duniho wrote:
On Sun, 31 Aug 2008 22:01:56 -0700, John B <jb******@yahoo.comwrote:
>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.

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

-- Posted on news://freenews.netfront.net - Complaints to ne**@netfront.net --
Sep 1 '08 #3
On Sun, 31 Aug 2008 22:32:42 -0700, John B <jb******@yahoo.comwrote:
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.
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
Sep 1 '08 #4
Peter Duniho wrote:
On Sun, 31 Aug 2008 22:32:42 -0700, John B <jb******@yahoo.comwrote:
>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.

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
Thanks for your response.

JB

-- Posted on news://freenews.netfront.net - Complaints to ne**@netfront.net --
Sep 1 '08 #5
Peter Duniho wrote:
On Sun, 31 Aug 2008 22:32:42 -0700, John B <jb******@yahoo.comwrote:
>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.

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.
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
Sep 7 '08 #6

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...
2
by: Sterling Ledet | last post by:
I am trying to create a web service that takes a string from my web server in the following format: Mon, 6 Oct 2003 18:39:47 UTC and put's in a datetime so it can then be reformatted in C# as...
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...
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...
1
by: Erica | last post by:
Hi. I have a string: "11/24/2006 23:59" named StartDateTime I need the string to be converted into a DateTime object in the following format: 2006-11-24 11:59 PM I tried the following:...
11
by: Peter Holschbach | last post by:
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...
0
by: Mushico | last post by:
How to calculate date of retirement from date of birth
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...

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.