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?
Jimmy Sep 02 '09 at 16:07 2009-09-02 16:07
source share