Reputation: 251
I'm trying to parse a DateTime in .NET 4.5. This is the code.
var rawDatetime = "10-11-2012, 11:19 AM";
var format = "MM-dd-yyyy, hh:mm tt";
var ok = DateTime.TryParseExact(rawDateTime, format, new CultureInfo("en-US"), DateTimeStyles.None, out dateTime);
This gives ok==false
, and dateTime=010101
. What am I doing wrong? Does the framework have a bug?
Fixed with "DateTime.TryParseExact(rawDateTime, format, CultureInfo.InvariantCulture, DateTimeStyles.None, out dateTime);"
Upvotes: 0
Views: 425
Reputation: 1063088
Are you sure this isn't the typo of rawDatetime
vs rawDateTime
(which are different variables and could have different values)?
It works fine for me:
var rawDateTime = "10-11-2012, 11:19 AM";
var format = "MM-dd-yyyy, hh:mm tt";
DateTime dateTime;
var ok = DateTime.TryParseExact(rawDateTime, format, new CultureInfo("en-US"),
DateTimeStyles.None, out dateTime);
if (ok)
{ // following prints correctly
Console.WriteLine(dateTime);
}
Upvotes: 3