[英]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.
字符串未被识别为有效的 DateTime。
I really don't understand.我真的不明白。
There are some zero-width Unicode characters in your strings.您的字符串中有一些零宽度 Unicode 字符。 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  
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.你可能在你的日期和时间之间有这些之一......特别是如果你从一个 html 文档中提取......哦,痛苦,痛苦。 The non-breaking-space also gets treated as whitespace in a regex and pass through undetected.
不间断空格也被视为正则表达式中的空格,并通过未被检测到。
text = text.Replace('\u00A0',' ');
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.