簡體   English   中英

字符串未被識別為有效的 DateTime

[英]String was not recognized as a valid DateTime

我正在將英國日期格式字符串轉換為美國格式以將其保存到數據庫中,但它向我拋出錯誤“字符串未被識別為有效的日期時間”。

string dateString = "13/06/2011";
DateTime dt = DateTime.Parse(dateString);

我也試過這個但同樣的例外。

DateTime aa = DateTime.ParseExact(dateString, "MM/dd/yyyy", new System.Globalization.CultureInfo("en-GB"));

請讓我知道如何將字符串中的 uk 格式日期轉換為我們的日期格式。

謝謝。

您指定了錯誤的格式。 它應該是dd/MM/yyyy

var dateString = "13/06/2011";
var aa = DateTime.ParseExact(dateString, "dd/MM/yyyy", CultureInfo.CurrentCulture);

具有 en-GB 文化的DateTime.Parse可以正常工作:

string dateString = "13/06/2011";

DateTime aa = DateTime.Parse(dateString, new CultureInfo("en-GB"));
// aa.Day == 13
// aa.Month == 6
// aa.Year == 2011

string result = aa.ToString("d", new CultureInfo("en-US"));
// result == "6/13/2011"

嘗試這個

DateTime dt = DateTime.Parse(dtString,
System.Threading.Tread.CurrentThread.CurrentCultur e.DateTimeFormat);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

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