TimeZoneInfo.IsDaylightSavingTime 方法      
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
指示指定的日期和时间是否处于当前 TimeZoneInfo 对象时区的夏令制范围内。
重载
| IsDaylightSavingTime(DateTime) | 指示指定的日期和时间是否处于当前 TimeZoneInfo 对象时区的夏令制范围内。 | 
| IsDaylightSavingTime(DateTimeOffset) | 指示指定的日期和时间是否处于当前 TimeZoneInfo 对象时区的夏令制范围内。 | 
IsDaylightSavingTime(DateTime)
- Source:
- TimeZoneInfo.cs
- Source:
- TimeZoneInfo.cs
- Source:
- TimeZoneInfo.cs
- Source:
- TimeZoneInfo.cs
指示指定的日期和时间是否处于当前 TimeZoneInfo 对象时区的夏令制范围内。
public:
 bool IsDaylightSavingTime(DateTime dateTime);public bool IsDaylightSavingTime(DateTime dateTime);member this.IsDaylightSavingTime : DateTime -> boolPublic Function IsDaylightSavingTime (dateTime As DateTime) As Boolean参数
- dateTime
- DateTime
日期和时间值。
返回
如果 dateTime 参数为夏令制,则为 true;否则为 false。
例外
示例
以下示例定义了一个名为 DisplayDateWithTimeZoneName 的方法,该方法使用 TimeZoneInfo.IsDaylightSavingTime 方法确定是显示时区的标准时间名称还是夏令时名称。
private void DisplayDateWithTimeZoneName(DateTime date1, TimeZoneInfo timeZone)
{
   Console.WriteLine("The time is {0:t} on {0:d} {1}", 
                     date1, 
                     timeZone.IsDaylightSavingTime(date1) ?
                         timeZone.DaylightName : timeZone.StandardName);   
}
// The example displays output similar to the following:
//    The time is 1:00 AM on 4/2/2006 Pacific Standard Time
let displayDateWithTimeZoneName (date1: DateTime) (timeZone: TimeZoneInfo) =
    printfn $"The time is {date1:t} on {date1:d} {if timeZone.IsDaylightSavingTime date1 then timeZone.DaylightName else timeZone.StandardName}" 
// The example displays output similar to the following:
//    The time is 1:00 AM on 4/2/2006 Pacific Standard Time
Private Sub DisplayDateWithTimeZoneName(date1 As Date, timeZone As TimeZoneInfo)
   Console.WriteLine("The time is {0:t} on {0:d} {1}", _
                     date1, _
                     IIf(timeZone.IsDaylightSavingTime(date1), _
                         timezone.DaylightName, timezone.StandardName))   
End Sub
' The example displays output similar to the following:
'    The time is 1:00 AM on 4/2/2006 Pacific Standard Time
注解
的 TimeZoneInfo.IsDaylightSavingTime 返回值受 对象表示 TimeZoneInfo 的时区与 Kind 参数的 dateTime 属性之间的关系的影响,如下表所示。
| TimeZoneInfo 对象 | DateTime.Kind 属性 | 结果 | 
|---|---|---|
| TimeZoneInfo.Local | DateTimeKind.Local | 确定 dateTime是否为夏令时。 | 
| TimeZoneInfo.Local | DateTimeKind.Utc | dateTime从协调世界时 (UTC) 转换为本地时间,并确定它是否为夏令时。 | 
| TimeZoneInfo.Local | DateTimeKind.Unspecified | 假定 表示 dateTime本地时间,并确定它是否为夏令时。 | 
| TimeZoneInfo.Utc | DateTimeKind.Local、DateTimeKind.Unspecified或DateTimeKind.Utc | false返回 (UTC 不支持夏令时) 。 | 
| 任何其他 TimeZoneInfo 对象。 | DateTimeKind.Local | 将本地时间转换为对象的等效时间 TimeZoneInfo ,然后确定后者是否为夏令时。 | 
| 任何其他 TimeZoneInfo 对象。 | DateTimeKind.Utc | 将 UTC 转换为对象的等效时间 TimeZoneInfo ,然后确定后者是否为夏令时。 | 
| 任何其他 TimeZoneInfo 对象。 | DateTimeKind.Unspecified | 确定 dateTime是否为夏令时。 | 
如果 对象表示的 TimeZoneInfo 时区不支持夏令时,则 方法始终返回 false。 许多时区(包括 Utc)不遵循夏令时。  若要确定时区是否支持夏令时,请检索其 SupportsDaylightSavingTime 属性的值。
              dateTime如果 参数指定当前对象的时区中的不明确时间,则 TimeZoneInfo.IsDaylightSavingTime 方法将解释dateTime为标准时间,如果其 Kind 属性为 或 DateTimeKind.Unspecified,DateTimeKind.Local则返回 false 。 
              Kind如果 属性为 DateTimeKind.Utc,此方法将选择正确的不明确时间,并指示它是否为夏令时。
              TimeZoneInfo.IsDaylightSavingTime(DateTime)由于 方法可以返回false不明确的日期和时间, (即表示特定时区) TimeZoneInfo.IsAmbiguousTime(DateTime) 中的标准时间或夏令时,因此该方法可以与 IsDaylightSavingTime(DateTime) 方法配对,以确定某个时间是否可能是夏令时。 由于不明确的时间可以是夏令时和标准时间, IsAmbiguousTime(DateTime) 因此可以先调用 方法来确定日期和时间是否为夏令时。 如果方法返回 false, IsDaylightSavingTime(DateTime) 则可以调用 方法来确定该值是否 DateTime 为夏令时。 以下示例演示了此方法。
DateTime unclearDate = new DateTime(2007, 11, 4, 1, 30, 0);
// Test if time is ambiguous.
Console.WriteLine("In the {0}, {1} is {2}ambiguous.", 
                  TimeZoneInfo.Local.DisplayName, 
                  unclearDate, 
                  TimeZoneInfo.Local.IsAmbiguousTime(unclearDate) ? "" : "not ");
// Test if time is DST.
Console.WriteLine("In the {0}, {1} is {2}daylight saving time.", 
                  TimeZoneInfo.Local.DisplayName, 
                  unclearDate, 
                  TimeZoneInfo.Local.IsDaylightSavingTime(unclearDate) ? "" : "not ");
Console.WriteLine();    
// Report time as DST if it is either ambiguous or DST.
if (TimeZoneInfo.Local.IsAmbiguousTime(unclearDate) || 
    TimeZoneInfo.Local.IsDaylightSavingTime(unclearDate))
    Console.WriteLine("{0} may be daylight saving time in {1}.", 
                      unclearDate, TimeZoneInfo.Local.DisplayName);  
// The example displays the following output:
//    In the (GMT-08:00) Pacific Time (US & Canada), 11/4/2007 1:30:00 AM is ambiguous.
//    In the (GMT-08:00) Pacific Time (US & Canada), 11/4/2007 1:30:00 AM is not daylight saving time.
//    
//    11/4/2007 1:30:00 AM may be daylight saving time in (GMT-08:00) Pacific Time (US & Canada).
let unclearDate = DateTime(2007, 11, 4, 1, 30, 0)
// Test if time is ambiguous.
printfn $"""In the {TimeZoneInfo.Local.DisplayName}, {unclearDate} is {if TimeZoneInfo.Local.IsAmbiguousTime unclearDate then "" else "not "}ambiguous."""
// Test if time is DST.
printfn $"""In the {TimeZoneInfo.Local.DisplayName}, {unclearDate} is {if TimeZoneInfo.Local.IsDaylightSavingTime unclearDate then "" else "not "}daylight saving time.
"""
// Report time as DST if it is either ambiguous or DST.
if TimeZoneInfo.Local.IsAmbiguousTime unclearDate || TimeZoneInfo.Local.IsDaylightSavingTime unclearDate then
    printfn $"{unclearDate} may be daylight saving time in {TimeZoneInfo.Local.DisplayName}."
// The example displays the following output:
//    In the (GMT-08:00) Pacific Time (US & Canada), 11/4/2007 1:30:00 AM is ambiguous.
//    In the (GMT-08:00) Pacific Time (US & Canada), 11/4/2007 1:30:00 AM is not daylight saving time.
//    
//    11/4/2007 1:30:00 AM may be daylight saving time in (GMT-08:00) Pacific Time (US & Canada).
Dim unclearDate As Date = #11/4/2007 1:30AM#
' Test if time is ambiguous.
Console.WriteLine("In the {0}, {1} is {2}ambiguous.", _ 
                  TimeZoneInfo.Local.DisplayName, _
                  unclearDate, _
                  IIf(TimeZoneInfo.Local.IsAmbiguousTime(unclearDate), "", "not "))
' Test if time is DST.
Console.WriteLine("In the {0}, {1} is {2}daylight saving time.", _ 
                  TimeZoneInfo.Local.DisplayName, _
                  unclearDate, _
                  IIf(TimeZoneInfo.Local.IsDaylightSavingTime(unclearDate), "", "not "))
Console.WriteLine()    
' Report time as DST if it is either ambiguous or DST.
If TimeZoneInfo.Local.IsAmbiguousTime(unclearDate) OrElse _ 
   TimeZoneInfo.Local.IsDaylightSavingTime(unclearDate) Then
    Console.WriteLine("{0} may be daylight saving time in {1}.", _ 
                      unclearDate, TimeZoneInfo.Local.DisplayName)                                           
End If
' The example displays the following output:
'    In the (GMT-08:00) Pacific Time (US & Canada), 11/4/2007 1:30:00 AM is ambiguous.
'    In the (GMT-08:00) Pacific Time (US & Canada), 11/4/2007 1:30:00 AM is not daylight saving time.
'    
'    11/4/2007 1:30:00 AM may be daylight saving time in (GMT-08:00) Pacific Time (US & Canada).
              dateTime如果 参数指定了无效时间,则如果参数Kind的 dateTime 属性值为 DateTimeKind.Local,则方法调用将ArgumentException引发 ;否则,该方法返回 false。
TimeZoneInfo.IsDaylightSavingTime调用 方法以确定在显示时区名称时是使用时区StandardName的值还是其DaylightName值。 有关插图,请参阅示例部分。
另请参阅
适用于
IsDaylightSavingTime(DateTimeOffset)
- Source:
- TimeZoneInfo.cs
- Source:
- TimeZoneInfo.cs
- Source:
- TimeZoneInfo.cs
- Source:
- TimeZoneInfo.cs
指示指定的日期和时间是否处于当前 TimeZoneInfo 对象时区的夏令制范围内。
public:
 bool IsDaylightSavingTime(DateTimeOffset dateTimeOffset);public bool IsDaylightSavingTime(DateTimeOffset dateTimeOffset);member this.IsDaylightSavingTime : DateTimeOffset -> boolPublic Function IsDaylightSavingTime (dateTimeOffset As DateTimeOffset) As Boolean参数
- dateTimeOffset
- DateTimeOffset
日期和时间值。
返回
如果 dateTimeOffset 参数为夏令制,则为 true;否则为 false。
注解
的 TimeZoneInfo.IsDaylightSavingTime 返回值受 对象表示 TimeZoneInfo 的时区与 Offset 参数的 dateTimeOffset 属性之间的关系的影响。 如果 dateTimeOffset 不对应于当前时区与协调世界时 (UTC) 的偏移量,该方法会将该时间转换为当前时区中的时间。 然后,它确定该日期和时间是否为夏令时。
如果 对象表示的 TimeZoneInfo 时区不支持夏令时,则 方法始终返回 false。 若要确定时区是否支持夏令时,请检索其 SupportsDaylightSavingTime 属性的值。