首页 > 解决方案 > 如何知道给定日期是 UTC 还是 C# 中的本地日期

问题描述

我在邮递员中传递 Date Like DateTimeOffSet 之类的日期,我只想找出日期是 utc 还是 Local,

{
  "fromDate": "2019-11-06T09:55:51.695Z",
  "toDate": "2019-11-06T09:55:51.695Z"
}

我试过的是,

var fromDateKind = fromDate.UtcDateTime.Kind;

var toDateKind = toDate.UtcDateTime.Kind;

我需要检查给定的 FromDate 和 ToDate 是 UTC,如何知道给定的 Date 是 UTC 还是 Local

标签: c#

解决方案


我自己没有尝试过,但我认为 DateTime 有一个Kindenum 类型的属性DateTimeKind

DateTimeKind 枚举文档在这里

DateTime.Kind 属性文档在这里

它应该像if (dateTimeObject.Kind == DateTimeKind.Local) {...}


推荐阅读