TheGeekZn
TheGeekZn

Reputation: 3914

TryParseExact not parsing with specified format

I'm using the following code in linqpad (remove the .Dump() for native C#):

string dateTime =   "3/20/2015 1:45:00 PM";
string dateFormat = "M/d/yyyy hh:mm:ss tt";

DateTime timeResult;
bool parsed = DateTime.TryParseExact(dateTime, dateFormat, System.Globalization.CultureInfo.InvariantCulture, System.Globalization.DateTimeStyles.AssumeLocal, out timeResult);

parsed.Dump();
timeResult.Dump();

However, the parse is false and the date is 0001-01-01 12:00:00 AM.

I can't see any issue in my format string. I tried updating it to "MM/dd/yyyy hh:mm:ss tt" with no changing effect.

Could anyone tell me where I'm going wrong?

Upvotes: 1

Views: 97

Answers (1)

shree.pat18
shree.pat18

Reputation: 21757

Change the string to ""M/d/yyyy h:mm:ss tt";" with a single h. For that matter, you might as well have to change to h:m:s if your minutes and seconds are in that format too.

Upvotes: 3

Related Questions