如何将“20161114T000000Z”解析为 sitecore 中的 DateTime 对象

How to parse "20161114T000000Z" to DateTime object in sitecore

我通过查询检索到的sitecore字段中的日期时间类型是“20161114T000000Z”字符串。

问题:

  1. 什么是 T 和 Z?

  2. 应该如何解析为DateTime对象呢?

看了一些相关问题,发现this thread中的DateTime.TryParseExact()可以解决问题,但还是不知道T和Z是什么意思。

请提供一些示例代码。

T 只是一个分隔符。 Z 告诉我们时间是 UTC 时间。我们正在查看的是 ISO-8601 Combined Date and Time format.

的变体

要解析此字符串,您可以使用

using System.Globalization;

var s = "20161114T000000Z";
var d = DateTime.ParseExact(s, "yyyyMMdd'T'HHmmss'Z'", CultureInfo.InvariantCulture);

Microsoft 的最新 guidance is to prefer the DateTimeOffset structure 用于明确存储时间点,因此也许您应该改用它:

var s = "20161114T000000Z";
var d = DateTimeOffset.ParseExact(s, "yyyyMMdd'T'HHmmss'Z'", CultureInfo.InvariantCulture);