我对日期时间的string
表示是"20181230183000000"
,我无法将转换为DateTime
:
DateTimeOffset.ParseExact(DOCDate, "dd.MM.yyyy HH:mm:ss.fff z", CultureInfo.InvariantCulture
答案 0 :(得分:4)
实际上,格式和模式应该匹配:您当前的输入
20181230183000000
表示
yyyyMMddHHmmssfff
代表year
,month
,day
,hours
,minutes
,seconds
,fractions of second
,所有< em>前导零和没有任何分隔符
代码:
string DOCDate = "20181230183000000";
DateTimeOffset result = DateTimeOffset.ParseExact(
DOCDate,
"yyyyMMddHHmmssfff",
CultureInfo.InvariantCulture,
DateTimeStyles.AssumeUniversal); // Since you've mentioned 'z' in the question
让我们看看
Console.Write(result.ToString(CultureInfo.InvariantCulture));
结果:
12/30/2018 18:30:00 +00:00
与DateTime
类似的代码:
DateTime result = DateTime.ParseExact(
DOCDate,
"yyyyMMddHHmmssfff",
CultureInfo.InvariantCulture,
DateTimeStyles.AssumeUniversal); // Since you've mentioned 'z' in the question
最后,如果您想以某种格式代表 DateTimeOffset
(DateTime
),请使用ToString()
方法:
Console.Write(result.ToString("dd.MM.yyyy HH:mm:ss.fff' z'"));
答案 1 :(得分:1)
您可以尝试:
first += second ;
first -= second ;
这是有关使用DateTimeOffset,DateTime而非TimeZoneInfo的好文章
DateTime.Parse("2012-09-30T23:00:00.0000000Z") always converts to DateTimeKind.Local