IsDaylightSavingTime 在 1:00 而不是 2:00 触发
IsDaylightSavingTime is triggered at 1:00 instead of 2:00
我只是想确定给定日期是否在夏令时之内。 2017 年,11 月 5 日 2:00 上午应该是触发点。
- 如果我的输入是 2017 年 11 月 5 日 00:00:00 DST 的输出为真
- 如果我的输入是 11/5/17 01:00:00 DST 的输出是假的
我原以为夏令时凌晨 1 点是真的,凌晨 2 点是假的
这是我的代码
var dateTime = new DateTime(2017,11,5,0,0,0);
var targetTimeZone = TimeZoneInfo.FindSystemTimeZoneById("Central Standard Time");
if (targetTimeZone.IsDaylightSavingTime(dateTime)) {
Console.WriteLine("Daylight Saving Time");
}
来自 the documentation(在备注部分):
If the dateTime
parameter specifies an ambiguous time in the current object's time zone, the TimeZoneInfo.IsDaylightSavingTime
method interprets dateTime
as standard time and returns false if its Kind
property is DateTimeKind.Local
or DateTimeKind.Unspecified
. If the Kind property is DateTimeKind.Utc
, this method will select the correct ambiguous time and indicate whether it is a daylight saving time.
然后继续解释 IsAmbiguousTime
,以及一个很长的示例,展示了如何使用它们,非常符合您描述的场景。
另请注意,接受 DateTimeOffset
参数的 IsDaylightSavingTime
的重载没有此问题,因为存在偏移量时值不能不明确 - 类似于 Kind 为 UTC 时。
我只是想确定给定日期是否在夏令时之内。 2017 年,11 月 5 日 2:00 上午应该是触发点。
- 如果我的输入是 2017 年 11 月 5 日 00:00:00 DST 的输出为真
- 如果我的输入是 11/5/17 01:00:00 DST 的输出是假的
我原以为夏令时凌晨 1 点是真的,凌晨 2 点是假的
这是我的代码
var dateTime = new DateTime(2017,11,5,0,0,0);
var targetTimeZone = TimeZoneInfo.FindSystemTimeZoneById("Central Standard Time");
if (targetTimeZone.IsDaylightSavingTime(dateTime)) {
Console.WriteLine("Daylight Saving Time");
}
来自 the documentation(在备注部分):
If the
dateTime
parameter specifies an ambiguous time in the current object's time zone, theTimeZoneInfo.IsDaylightSavingTime
method interpretsdateTime
as standard time and returns false if itsKind
property isDateTimeKind.Local
orDateTimeKind.Unspecified
. If the Kind property isDateTimeKind.Utc
, this method will select the correct ambiguous time and indicate whether it is a daylight saving time.
然后继续解释 IsAmbiguousTime
,以及一个很长的示例,展示了如何使用它们,非常符合您描述的场景。
另请注意,接受 DateTimeOffset
参数的 IsDaylightSavingTime
的重载没有此问题,因为存在偏移量时值不能不明确 - 类似于 Kind 为 UTC 时。