簡體   English   中英

字符串未被識別為格式為“MM/dd/yyy hh:mm:ss tt”的有效日期時間

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM