简体   繁体   中英

Datetime.ParseExact “String was not recognized as a valid DateTime” error

Why i can't parse a string like this:

DateTime date = DateTime.ParseExact("‎23.‎02.‎2016 08:59:35", 
                  "dd.MM.yyyy HH:mm:ss", CultureInfo.InvariantCulture);

It is throwing an exception

String was not recognized as a valid DateTime.

I really don't understand.

There are some zero-width Unicode characters in your strings. If you remove them it will work:

DateTime.ParseExact("23.02.2016 08:59:35",
    "dd.MM.yyyy HH:mm:ss", CultureInfo.InvariantCulture)

Beware beware the &nbsp non-breaking-space it looks like a space but isn't. You might have one of these between your date and time.... Especially if your pulling from a html document...Ohh the pain, the pain. The non-breaking-space also gets treated as whitespace in a regex and pass through undetected.

text = text.Replace('\u00A0',' ');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM