日期时间的参数不正确问题



我有以下行。 DateTime.ParseExact("08-11-2013 07:38:05", "yyyy-MM-dd HH:mm:ss", Nothing)

它会引发错误。

参数不正确。

堆栈跟踪

at System.DateTimeParse.ParseExact(String s, String format, DateTimeFormatInfo dtfi, DateTimeStyles style) at System.DateTime.ParseExact(String s, String format, IFormatProvider 提供者)

提前谢谢。

您的格式与您提供的日期不匹配,它应该是:

"dd-MM-yyyy HH:mm:ss"

考虑到你的意思 十一月 8, 2013

您的代码应该是:

DateTime dt = DateTime.ParseExact("08-11-2013 07:38:05", 
                                  "dd-MM-yyyy HH:mm:ss", 
                                   CultureInfo.InvariantCulture); // Instead of Nothing

您也可以使用"d-M-yyyy HH:mm:ss"格式,因为它将考虑个位数/两位数的日期和月份。

此外,您似乎来自 VB.Net 后台,其中Nothing是默认值,在 C# 中,您可以使用null的情况或更好地使用CultureInfo.InvariantCulture

有关详细信息,请参阅: 自定义日期和时间格式字符串

最新更新