簡體   English   中英

使用格式 'dd.MM.yyyy HH:mm:ss.fff z' 將字符串轉換為日期

[英]Convert string to date with format 'dd.MM.yyyy HH:mm:ss.fff z'

我的日期字符串是“2018-12-30T18:30:00.000Z”,我無法將其轉換為日期時間。

我試過了

DateTimeOffset.ParseExact(DOCDate, "dd.MM.yyyy HH:mm:ss.fff z", CultureInfo.InvariantCulture);

您的代碼現在可以工作的原因是字符串中的日期采用yyyy-MM-ddTHH:mm:ss.fffZ格式,而您指定給ParseExact的格式為dd.MM.yyyy HH:mm:ss.fff z 這樣就無法解析日期。

DateTimeOffset.Parse("2018-12-30T18:30:00.000Z"); 似乎工作正常。

  string s="2018-12-30T18:30:00.000Z";
   DateTime da=     DateTime.ParseExact(s, "yyyy-MM-ddTHH:mm:ss.fffZ", CultureInfo.InvariantCulture);
   Response.Write(da.ToString());

嘗試這個

您可以使用DateTime.TryParseDateTimeStyles.AdjustToUniversal以獲得完全相同的時間,或使用DateTimeStyles.AssumeUniversal來調整到本地時間:

 bool result = DateTime.TryParse("2018-12-30T18:30:00.000Z", CultureInfo.CurrentCulture, 
                                 DateTimeStyles.AdjustToUniversal, out DateTime dt);

=> 12/30/2018 18:30:00 or 30/12/2018 18:30:00 (...)

 bool result = DateTime.TryParse("2018-12-30T18:30:00.000Z", CultureInfo.CurrentCulture, 
                                 DateTimeStyles.AssumeUniversal, out DateTime dt);

=> 30/12/2018 [Adjusted to your Local time] (...)

這將為您提供所需格式的字符串

(DateTime.Parse("2018-12-30T18:30:00.000Z")).ToString("dd.MM.yyyy HH:mm:ss.fff z");

string dateString="2018-12-30T18:30:00.000z";
Console.WriteLine(DateTimeOffset.Parse(dateString));

請參閱下圖中的輸出它正在工作在此處輸入圖片說明

暫無
暫無

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

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