[英]input string was not in a correct format date time c#
我從 URL 獲取日期時間作為字符串並將其轉換為日期時間格式
我得到這個例外
輸入字符串的格式不正確
這是我試過的代碼
string time = expiredorno;
var result = Convert.ToDateTime(time);
string timeleft = result.ToString("dd/MM/yyyy HH:mm:ss tt", CultureInfo.CurrentCulture);
注意:在代碼中變量'expiredorno'有這個值“2021/05/03 14:54:14 PM”
在默認區域性設置下,輸入字符串格式可能無效。 您可以按照此處指定的方式設置 convert 方法的區域性:
CultureInfo culture = new CultureInfo("en-US");
Convert.ToDateTime(time, culture);
string time = "2021/05/03 14:54:14 PM";
var result = Convert.ToDateTime(time, CultureInfo.InvariantCulture);
string timeleft = result.ToString("dd/MM/yyyy HH:mm:ss tt", CultureInfo.InvariantCulture);
我們可以嘗試添加 CultureInfo.InvariantCulture。 我們可以使用 DateTime Struct (System) | 中正確的解析方法之一。 微軟文檔https://docs.microsoft.com/en-us/dotnet/api/system.datetime?view=netframework-4.7.2
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.