TimeZoneInfo.IsDaylightSavingTime 方法

定義

表示指定的日期和時間是否在目前 TimeZoneInfo 物件時區的日光節約時間範圍內。

多載

IsDaylightSavingTime(DateTime)

表示指定的日期和時間是否在目前 TimeZoneInfo 物件時區的日光節約時間範圍內。

IsDaylightSavingTime(DateTimeOffset)

表示指定的日期和時間是否在目前 TimeZoneInfo 物件時區的日光節約時間範圍內。

IsDaylightSavingTime(DateTime)

Source:
TimeZoneInfo.cs
Source:
TimeZoneInfo.cs
Source:
TimeZoneInfo.cs

表示指定的日期和時間是否在目前 TimeZoneInfo 物件時區的日光節約時間範圍內。

public:
 bool IsDaylightSavingTime(DateTime dateTime);
public bool IsDaylightSavingTime (DateTime dateTime);
member this.IsDaylightSavingTime : DateTime -> bool
Public Function IsDaylightSavingTime (dateTime As DateTime) As Boolean

參數

dateTime
DateTime

日期和時間值。

傳回

如果 dateTime 參數為日光節約時間則為 true,否則為 false

例外狀況

dateTime 值的 Kind 屬性是 Local,而且 dateTime 是無效的時間。

範例

下列範例會定義名為 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.LocalDateTimeKind.UnspecifiedDateTimeKind.Utc false傳回 (UTC 不支援日光節約時間) 。
任何其他 TimeZoneInfo 物件。 DateTimeKind.Local 將當地時間轉換為物件的對等時間 TimeZoneInfo ,然後判斷後者是否為日光節約時間。
任何其他 TimeZoneInfo 物件。 DateTimeKind.Utc 將 UTC 轉換為物件的對等時間 TimeZoneInfo ,然後判斷後者是否為日光節約時間。
任何其他 TimeZoneInfo 物件。 DateTimeKind.Unspecified 判斷是否 dateTime 為日光節約時間。

如果 物件所 TimeZoneInfo 代表的時區不支援日光節約時間,方法一律會傳 false 回 。 許多時區,包括 Utc ,不會觀察日光節約時間。 若要判斷時區是否支援日光節約時間,請擷取其 SupportsDaylightSavingTime 屬性的值。

dateTime如果 參數在目前物件的時區中指定模棱兩可的時間,則方法會解譯 dateTime 為標準時間, TimeZoneInfo.IsDaylightSavingTime 並在 其 Kind 屬性為 DateTimeKind.LocalDateTimeKind.Unspecified 時傳 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如果參數指定了不正確時間,如果 dateTime 參數的 Kind 屬性值為 DateTimeKind.Local ,則方法呼叫會擲 ArgumentException 回 ,否則方法會傳 false 回 。

呼叫 方法, TimeZoneInfo.IsDaylightSavingTime 以判斷在顯示時區名稱時,是否要使用 StandardName 時區的值或其 DaylightName 值。 如需圖例,請參閱一節。

另請參閱

適用於

IsDaylightSavingTime(DateTimeOffset)

Source:
TimeZoneInfo.cs
Source:
TimeZoneInfo.cs
Source:
TimeZoneInfo.cs

表示指定的日期和時間是否在目前 TimeZoneInfo 物件時區的日光節約時間範圍內。

public:
 bool IsDaylightSavingTime(DateTimeOffset dateTimeOffset);
public bool IsDaylightSavingTime (DateTimeOffset dateTimeOffset);
member this.IsDaylightSavingTime : DateTimeOffset -> bool
Public Function IsDaylightSavingTime (dateTimeOffset As DateTimeOffset) As Boolean

參數

dateTimeOffset
DateTimeOffset

日期和時間值。

傳回

如果 dateTimeOffset 參數為日光節約時間則為 true,否則為 false

備註

TimeZoneInfo.IsDaylightSavingTime 傳回值會受到 物件所 TimeZoneInfo 表示之時區與 Offset 參數 屬性 dateTimeOffset 之間的關聯性所影響。 如果未 dateTimeOffset 對應到目前時區與國際標準時間 (UTC) 的位移,方法會將該時間轉換為目前時區中的時間。 然後,它會判斷該日期和時間是否為日光節約時間。

如果 物件所 TimeZoneInfo 代表的時區不支援日光節約時間,方法一律會傳 false 回 。 若要判斷時區是否支援日光節約時間,請擷取其 SupportsDaylightSavingTime 屬性的值。

另請參閱

適用於