解析日期时出现“字符串无法识别为有效日期”错误
Dim reviewDateString = "02/14/2019 15:06:02"
DateTime.ParseExact(reviewDateString, "MM/dd/yyyy HH:mm:ss",
System.Globalization.DateTimeFormatInfo.InvariantInfo)
我也尝试过MM / dd / yyyy H:mm:ss格式。那也不起作用。 谁能让我知道我正在做的错误。
答案 0 :(得分:3)
当我将您的确切代码粘贴到dotnetfiddle.net中时,我可以看到不可见的字符(以点表示)。您需要清洁reviewDateString
才能删除它们。
更新:添加了一个示例,该示例说明了如何使用正则表达式解析不可见的字符(无效的日期字符串字符)
正在工作的小提琴:https://dotnetfiddle.net/iQNhrp
Imports System
Imports System.Text.RegularExpressions
Public Module Module1
Public Sub Main()
Dim reviewDateString = "02/14/2019 15:06:02"
Dim parsedDateString = RegEx.Replace(reviewDateString, "[^0-9a-zA-Z :/]", String.Empty)
Console.WriteLine("Copy and paste this into dotnetfiddle to see it has invisible characters:")
Console.WriteLine(reviewDateString)
Console.WriteLine()
Console.WriteLine("Copy and paste this into dotnetfiddle to see it has no invisible characters, and is a valid date string.")
Console.WriteLine(parsedDateString)
' no parse errors
DateTime.ParseExact(parsedDateString, "MM/dd/yyyy HH:mm:ss", System.Globalization.DateTimeFormatInfo.InvariantInfo)
End Sub
End Module