在vb.net中设置要分析的字符串格式时遇到困难

在vb.net中设置要分析的字符串格式时遇到困难,vb.net,Vb.net,有时会出现错误:System.FormatException:“字符串未被识别为有效的日期时间。” 为什么??它似乎格式正确。VB.net 4.6.1 framework您是否尝试过使用CDate()函数 dim dt as string= "03/22/20 20:12:27.320" Dim tempDate As DateTime = DateTime.ParseExact(dt, "MM/dd/yy hh:mm:ss.fff", CultureInfo.GetCultureInfo("

有时会出现错误:System.FormatException:“字符串未被识别为有效的日期时间。”


为什么??它似乎格式正确。VB.net 4.6.1 framework

您是否尝试过使用CDate()函数

dim dt as string= "03/22/20 20:12:27.320"

Dim tempDate As DateTime = DateTime.ParseExact(dt, "MM/dd/yy hh:mm:ss.fff", CultureInfo.GetCultureInfo("en-US").DateTimeFormat)


“hh”是12小时格式。20小时显然不是12小时格式的有效小时。如果需要24小时格式,请使用“HH”。

这仅在系统区域性设置为US时有效。其他地方的日期都是月的前一天,因此使用CDate只能在一年中的12天内正常工作。剩下的时间,它要么失败,要么更糟,成功,但产生了错误的价值。
dim dt as string= "03/22/20 20:12:27.320"
dim tempDate as Datetime = Cdate(dt)
dim tempDate as Datetime = Cdate("03/22/20 20:12:27.320")