chuckd
chuckd

Reputation: 14610

String was not recognized as a valid date time with DateTime.ParseExact

Here is the string I'm passing into DateTime.ParseExact

var dateTime = "2015-04-07 0‎6‎:‎00‎:‎00‎ ‎AM";

here is the code

DateTime dateWithTime = DateTime.ParseExact(dateTime, "yyyy-MM-dd HH:mm:ss tt",
            CultureInfo.InvariantCulture);

I'm getting a format exception that says

"String was not recognized as a valid DateTime."

Upvotes: 0

Views: 339

Answers (1)

Aleksey Shubin
Aleksey Shubin

Reputation: 1998

You have invisible symbols in your dateTime string (left-to-right marks), you can see them if you look into HTML of this page. I don't sure if they are originally from your code, or added somehow when posted on SO, but when I copy it directly to Visual Studio, the same error occurs. When I re-type manually, it works.

Copy and paste it into your code:

var dateTime = "2015-04-07 06:00:00 AM";

Upvotes: 2

Related Questions