在C#中將帶時區的字串轉成DateTime型別需要用到DateTimeFormatInfo,這個類包含特定于區域性的資訊, 例如,將Sun, 28 Jun 2020 03:40:22 GMT轉成DateTime該怎么弄呢?
DateTimeFormatInfo dtFormat = new DateTimeFormatInfo(); dtFormat.FullDateTimePattern = "ddd, dd MMM yyyy HH':'mm':'ss 'GMT'"; var result = Convert.ToDateTime("Sun, 28 Jun 2020 03:40:22 GMT", dtFormat);FullDateTimePattern屬性為長日期和長時間值獲取或設定自定義格式字串,而“Sun, 28 Jun 2020 03:40:22 GMT”格式屬于RFC1123Pattern(該格式用于用于基于 Internet 工程任務組 (IETF) 征求意見檔案 (RFC) 1123 規范的時間值),這種格式是自定義格式字串, 轉換出的結果是:2020-06-28 11:40:22 轉換時自動加了8個小時的時差 參考: DateTimeFormatInfo 類 https://docs.microsoft.com/zh-cn/dotnet/api/system.globalization.datetimeformatinfo?view=netcore-3.1
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/9631.html
標籤:C#
