简体   繁体   English

Datetime.ParseExact“字符串未被识别为有效的日期时间”错误

[英]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 &nbsp non-breaking-space it looks like a space but isn't.当心当心&nbsp非中断空间它看起来像一个空间但不是。 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.

相关问题 在DateTime.ParseExact上未将字符串识别为有效的DateTime - String was not recognized as a valid DateTime on DateTime.ParseExact DateTime.ParseExact字符串未被识别为有效的日期时间 - DateTime.ParseExact String not recognized as valid datetime DateTime.ParseExact:字符串未被识别为有效的DateTime - DateTime.ParseExact: String was not recognized as a valid DateTime DateTime.ParseExact()-无法将字符串识别为有效的DateTime - DateTime.ParseExact() - String was not recognized as a valid DateTime DateTime.ParseExact给出错误:字符串未被识别为有效的DateTime - DateTime.ParseExact give the error: String was not recognized as a valid DateTime DateTime.ParseExact引发以下错误“字符串未被识别为有效的DateTime” - DateTime.ParseExact is raising the following error “String was not recognized as a valid DateTime” 无法将字符串识别为DateTime.ParseExact的有效参数 - String was not recognized as a valid parameter for DateTime.ParseExact DateTime.ParseExact FormatException字符串未被识别为有效的DateTime - DateTime.ParseExact FormatException String was not recognized as a valid DateTime 使用DateTime.ParseExact时,字符串未被识别为有效的DateTime - String was not recognized as a valid DateTime when using DateTime.ParseExact DateTime.ParseExact赋予String未被识别为有效的DateTime。 - DateTime.ParseExact gives String was not recognized as a valid DateTime.
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM