且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

DateTime.ParseExact:"字符串未被识别为有效的DateTime"

更新时间:2022-12-11 09:50:39

在你的字符串值 hourRegistration.Date 不是你认为它是。

Your string value in hourRegistration.Date is not what you think it is.

随着.NET串,还有一些要么不调试表示在所有出现许多Unicode字符或者事实上并非它们似乎是字符。在你的情况,由 ToCharArray 调试扩展显示,您的字符串实际上包含很多的 U + 200E Unicode字符左至右符号'(U + 200E)字符。

With strings in .NET, there are many Unicode characters which either do not appear at all in the debug representation, or are not in fact the characters they appear to be. In your case, as shown by the ToCharArray debug expansion, your string actually contains a number of U+200E Unicode Character 'LEFT-TO-RIGHT MARK' (U+200E) characters.

这些都是在调试代表性看不见,而是试图解析日期时是相关的。我不知道他们是如何到达那里或为什么他们在那里 - 这东西你需要进一步的研究。

These are invisible in the debug representation, but ARE relevant when trying to parse the date. I don't know how they got there or why they are there - that's something you'd need to research further.

要解决您的眼前问题,解析您的日期之前,从你的字符串中删除所有非ASCII字符,沿着线:

To solve your immediate issue, before parsing your date, remove all non-ASCII characters from your string, along the lines of:

var actualDateString = new String(hourRegistration.Date
                          .ToCharArray()
                          .Where(c => c <= 255)
                          .ToArray()
                       );



(我刚刚敲了这一点,所以它不是很漂亮)

(I've just banged this out so it's not very pretty)

那么你应该能够解析 actualDateString 的要求。

Then you should be able to parse actualDateString as required.