[英]String was not recognized as a valid DateTime with the format of “MM/dd/yyy hh:mm:ss tt”
我正在嘗試在 C# 中將字符串轉換為 DateTime,但出現此錯誤:
System.FormatException: 'String 未被識別為有效的 DateTime。'
錯誤在下一行:
DateTime endTime = DateTime.ParseExact(endDate, "MM/dd/yyyy hh:mm:ss tt", null);
我的endDate
變量具有以下信息:“10/03/2017 06:52:48 AM”
我做錯了什么?
當您使用null
作為IFormatProvider
,所有DateTime
解析方法都使用您計算機的CurrentCulture
設置。
有幾種可能您會遇到異常。 例如,您的CurrentCulture
可能沒有AM
因為它是AMDesignator
屬性。
相反,使用適當的文化,如InvariantCulture
。
DateTime endTime = DateTime.ParseExact("10/03/2017 06:52:48 AM",
"MM/dd/yyyy hh:mm:ss tt",
CultureInfo.InvariantCulture);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.