如果我有一个特定的TimeZoneInfo实例,如何在指定的时区创建一个新的DateTime实例?例如,如果我有:
var tz = TimeZoneInfo.FindSystemTimeZoneById("US Eastern Standard Time");
var date = new DateTime(2017, 1, 1, 0, 0, 0, DateTimeKind.Unspecified);
Console.WriteLine(TimeZoneInfo.ConvertTime(date, tz));
无论我定义的DateTimeKind(Utc,Local或Unspecified),我总是收到2016年12月31日晚上7:00:00。
我该如何声明一个新的DateTime,在美国东部标准时间下的2017年1月1日0:00:00?
TimeZoneInfo.BaseUtcOffset
,还有TimeZoneInfo.GetUtcOffset(DateTime)
和TimeZoneInfo.GetUtcOffset(DateTimeOffset)
。 - Der KommissarDateTime
转换为本地时间吗? - Federico DipumaDateTime
只区分本地时间和 UTC 时间,其中没有特定的时区信息。您可以将其从您的时区转换为 UTC(然后转换为本地时间,如果需要),或者使用DateTimeOffset
,即使它没有时区信息,但至少具有存储在 UTC 中的内置偏移量。此外,请注意,使用客户端时区存储时间戳几乎总是不明智的,最好存储 UTC 日期/时间,然后在客户端内部进行时区转换以进行可视化。 - Federico Dipuma