Why DateTime.ParseExact () cannot parse "9/1/2009" using "M / d / yyyy"

I have a line that looks like this: "9/1/2009". I want to convert it to a DateTime object (using C #).

It works:

DateTime.Parse("9/1/2009", new CultureInfo("en-US")); 

But I do not understand why this does not work:

 DateTime.ParseExact("9/1/2009", "M/d/yyyy", null); 

There is no word in the date (for example, "September"), and I know the specific format, so I would prefer to use ParseExact (and I do not understand why CultureInfo is needed). But I keep getting the scary exception "String was not recognized as a valid DateTime."

thank

A small continuation. Here are three approaches that work:

 DateTime.ParseExact("9/1/2009", "M'/'d'/'yyyy", null); DateTime.ParseExact("9/1/2009", "M/d/yyyy", CultureInfo.InvariantCulture); DateTime.Parse("9/1/2009", new CultureInfo("en-US")); 

And here are 3 that don't work:

 DateTime.ParseExact("9/1/2009", "M/d/yyyy", CultureInfo.CurrentCulture); DateTime.ParseExact("9/1/2009", "M/d/yyyy", new CultureInfo("en-US")); DateTime.ParseExact("9/1/2009", "M/d/yyyy", null); 

So Parse () works with "en-US", but not ParseExact ... Unexpectedly?

+51
datetime parsing cultureinfo
Sep 02 '09 at 16:07
source share
7 answers

I suspect that the problem is the slashes in the format string and those contained in the data. This culture separator character is in the format string, and a null ending argument means "use current culture". If you either escape the slashes ("M" / "d" / "yyyy") or specify CultureInfo.InvariantCulture , everything will be fine.

If anyone is interested in reproducing this:

 // Works DateTime dt = DateTime.ParseExact("9/1/2009", "M'/'d'/'yyyy", new CultureInfo("de-DE")); // Works DateTime dt = DateTime.ParseExact("9/1/2009", "M/d/yyyy", new CultureInfo("en-US")); // Works DateTime dt = DateTime.ParseExact("9/1/2009", "M/d/yyyy", CultureInfo.InvariantCulture); // Fails DateTime dt = DateTime.ParseExact("9/1/2009", "M/d/yyyy", new CultureInfo("de-DE")); 
+83
Sep 02 '09 at 16:14
source share
— -

I bet your computer culture is not "en-US". From the documentation :

If the provider is an empty reference (Nothing in Visual Basic), the current culture is used.

If your current culture is not "en-US", this explains why it works for me, but does not work for you. and it works when you explicitly indicate that the culture should be "en- USA".

+2
02 Sep '09 at 16:20
source share

Try

 Date.ParseExact("9/1/2009", "M/d/yyyy", new CultureInfo("en-US")) 
+1
Sep 02 '09 at 16:15
source share

try it

 provider = new CultureInfo("en-US"); DateTime.ParseExact("9/1/2009", "M/d/yyyy", provider); 

Bye

0
02 Sep '09 at 16:17
source share

I tried it on XP and it does not work if the PC is set to International time yyyy-Md. Place a breakpoint on the line, and before processing it, change the date line to use "-" instead of "/", and you will find that it works. It doesn't matter if you have CultureInfo or not. It seems strange that you can specify an experimental format just to ignore the delimiter.

0
Jul 14 '10 at 23:55
source share

Try:

Configure in web configuration file

<system.web> <globalization culture="ja-JP" uiCulture="zh-HK" /> </system.web>

for example: DateTime dt = DateTime.ParseExact ("08/21/2013", "MM / dd / yyyy", null);

ref url: http://support.microsoft.com/kb/306162/

-one
Mar 04 '15 at 9:09
source share

Set the DateTimePicker Format property to custom and CustomFormat prperty to M/dd/yyyy .

-2
Jul 24 2018-12-12T00:
source share



All Articles