您應該可以使用DateTimeOffset
和K
custom format specifier來格式化它。如果您願意,可以在之後將其轉換爲DateTime
。示例代碼:
using System;
using System.Globalization;
class Test
{
static void Main()
{
string text = "2013-07-03T02:16:03.000+01:00";
string pattern = "yyyy-MM-dd'T'HH:mm:ss.FFFK";
DateTimeOffset dto = DateTimeOffset.ParseExact
(text, pattern, CultureInfo.InvariantCulture);
Console.WriteLine(dto);
}
}
有一點要注意的是,這是嚴重命名 - 它實際上不是一個時間段,它只是一個UTC偏移。它不是實際上告訴你原來的時區。 (可以有多個不同的時區觀察同在同一時間偏移。)
或用Noda Time(不穩定的版本,這將很快成爲1.2):的
string text = "2013-07-03T02:16:03.000+01:00";
OffsetDateTimePattern pattern = OffsetDateTimePattern.ExtendedIsoPattern;
OffsetDateTime odt = pattern.Parse(text).Value;
Console.WriteLine(odt);
可能重複[DateTime.ParseExact ,忽略時區](http://stackoverflow.com/questions/6676856/datetime-parseexact-ignore-the-timezone) – V4Vendetta