all groups > dotnet clr > december 2003 >
You're in the

dotnet clr

group:

Problem with DateTime.ParseExact


Problem with DateTime.ParseExact Fred Strauss
12/12/2003 8:30:32 AM
dotnet clr:
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 same call to ParseExact to parse either format. I am
using C# to call the following form of ParseExact:

public static DateTime ParseExact(string, string[], IFormatProvider,
DateTimeStyles);

Examples of the 2 formats I'm passing in the source string are:

20031211T151302Z

and

2003-12-03T16:32:00-5:00

The format strings I'm passing in the 2nd argument are defined with:

private string[] sDateFormats = new string[] {"yyyyMMdd\\THHmmss\\Z",
"yyyy\\-MM\\-dd\\THH:mm:sszzz"};

The problem is that when I attempt to parse dates in the 2nd format a
FormatException is thrown.

Any help is greatly appreciated

Fred Strauss
Project Manager
Associated Press
212-621-1968
fred_strauss@ap.org


RE: Problem with DateTime.ParseExact amoore NO[at]SPAM online.microsoft.com
12/22/2003 11:42:36 PM
Hello Fred. I'm the development owner of the DateTime class.

I could not come up with an exlplanation for what you were seeing, so I got
your repro and debugged it. I think you have found a valid bug in the
product and I have fixed it for the next release (Whidbey).

The problem is that ParseExact cannot parse a value like "-5:00" for the
"zzz" format. It requires two leading digits like so: "-05:00". This is a
bug, because DateTime.Parse will accept a single leading digit, and the
ISO8601 allows it also.

Hopefully passing in "-05:00" can be an effective work-around to unblock
you here. In general, Parse and Exact are good at round-tripping a value
the exact same way that it is formatted by ToString. If you are having
trouble with a format, try calling ToString on the format to determine what
it is looking for.

Thanks very much for asking the question. It allows us to make the product
better, and will help you determine if it is pilot error. I hope this does
not inconvience you too much.

Anthony.

--------------------
| From: Fred Strauss <frauss@comcast.net>
| Subject: Problem with DateTime.ParseExact
| Date: Fri, 12 Dec 2003 08:30:32 -0500
| Message-ID: <pigjtvcorqt4ifc9q75tlpg9rkap7nj1j1@4ax.com>
| X-Newsreader: Forte Agent 1.93/32.576 English (American)
| MIME-Version: 1.0
| Content-Type: text/plain; charset=us-ascii
| Content-Transfer-Encoding: 7bit
| Newsgroups:
microsoft.public.dotnet.framework.clr,microsoft.public.dotnet.languages.csha
rp
| NNTP-Posting-Host: pcp01721375pcs.union01.nj.comcast.net 68.37.29.230
| Lines: 1
| Path:
cpmsftngxa07.phx.gbl!cpmsftngxa06.phx.gbl!TK2MSFTNGXS01.phx.gbl!TK2MSFTNGXA0
5.phx.gbl!TK2MSFTNGP08.phx.gbl!tk2msftngp13.phx.gbl
| Xref: cpmsftngxa07.phx.gbl
microsoft.public.dotnet.languages.csharp:205152
microsoft.public.dotnet.framework.clr:8979
| X-Tomcat-NG: microsoft.public.dotnet.framework.clr
|
| 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 same call to ParseExact to parse either format. I am
| using C# to call the following form of ParseExact:
|
| public static DateTime ParseExact(string, string[], IFormatProvider,
| DateTimeStyles);
|
| Examples of the 2 formats I'm passing in the source string are:
|
| 20031211T151302Z
|
| and
|
| 2003-12-03T16:32:00-5:00
|
| The format strings I'm passing in the 2nd argument are defined with:
|
| private string[] sDateFormats = new string[] {"yyyyMMdd\\THHmmss\\Z",
| "yyyy\\-MM\\-dd\\THH:mm:sszzz"};
|
| The problem is that when I attempt to parse dates in the 2nd format a
| FormatException is thrown.
|
| Any help is greatly appreciated
|
| Fred Strauss
| Project Manager
| Associated Press
| 212-621-1968
| fred_strauss@ap.org
|
|
|
|
Re: Problem with DateTime.ParseExact Fred Strauss
12/23/2003 6:52:03 AM
Anthony, thanks for getting back to me.

I've already got a workaround but it's nice to know I wasn't losing my
mind in thinking this should have worked.

Fred
On Mon, 22 Dec 2003 23:42:36 GMT, amoore@online.microsoft.com (Anthony
[quoted text, click to view]
AddThis Social Bookmark Button