संपादित करें

इसके माध्यम से साझा किया गया


TimeZoneInfo.IsDaylightSavingTime Method

Definition

Indicates whether a specified date and time falls in the range of daylight saving time for the current TimeZoneInfo object's time zone.

Overloads

IsDaylightSavingTime(DateTime)

Indicates whether a specified date and time falls in the range of daylight saving time for the time zone of the current TimeZoneInfo object.

IsDaylightSavingTime(DateTimeOffset)

Indicates whether a specified date and time falls in the range of daylight saving time for the time zone of the current TimeZoneInfo object.

IsDaylightSavingTime(DateTime)

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

Indicates whether a specified date and time falls in the range of daylight saving time for the time zone of the current TimeZoneInfo object.

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

Parameters

dateTime
DateTime

A date and time value.

Returns

true if the dateTime parameter is a daylight saving time; otherwise, false.

Exceptions

The Kind property of the dateTime value is Local and dateTime is an invalid time.

Examples

The following example defines a method named DisplayDateWithTimeZoneName that uses the TimeZoneInfo.IsDaylightSavingTime method to determine whether to display a time zone's standard time name or daylight saving time name.

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

Remarks

The return value of TimeZoneInfo.IsDaylightSavingTime is affected by the relationship between the time zone represented by the TimeZoneInfo object and the Kind property of the dateTime parameter, as the following table shows.

TimeZoneInfo object DateTime.Kind property Result
TimeZoneInfo.Local DateTimeKind.Local Determines whether dateTime is daylight saving time.
TimeZoneInfo.Local DateTimeKind.Utc Converts dateTime from Coordinated Universal Time (UTC) to local time and determines whether it is daylight saving time.
TimeZoneInfo.Local DateTimeKind.Unspecified Assumes that dateTime represents local time and determines whether it is daylight saving time.
TimeZoneInfo.Utc DateTimeKind.Local, DateTimeKind.Unspecified, or DateTimeKind.Utc Returns false (UTC does not support daylight saving time).
Any other TimeZoneInfo object. DateTimeKind.Local Converts the local time to the equivalent time of the TimeZoneInfo object and then determines whether the latter is daylight saving time.
Any other TimeZoneInfo object. DateTimeKind.Utc Converts UTC to the equivalent time of the TimeZoneInfo object and then determines whether the latter is daylight saving time.
Any other TimeZoneInfo object. DateTimeKind.Unspecified Determines whether dateTime is daylight saving time.

If the time zone represented by the TimeZoneInfo object does not support daylight saving time, the method always returns false. A number of time zones, including Utc, do not observe daylight saving time. To determine whether a time zone supports daylight saving time, retrieve the value of its SupportsDaylightSavingTime property.

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.

Because the TimeZoneInfo.IsDaylightSavingTime(DateTime) method can return false for a date and time that is ambiguous (that is, a date and time that can represent either a standard time or a daylight saving time in a particular time zone), the TimeZoneInfo.IsAmbiguousTime(DateTime) method can be paired with the IsDaylightSavingTime(DateTime) method to determine whether a time may be a daylight saving time. Because an ambiguous time is one that can be both a daylight saving time and a standard time, the IsAmbiguousTime(DateTime) method can be called first to determine whether a date and time may be a daylight saving time. If the method returns false, the IsDaylightSavingTime(DateTime) method can be called to determine whether the DateTime value is a daylight saving time. The following example illustrates this technique.

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).

If the dateTime parameter specifies an invalid time, the method call throws an ArgumentException if the value of the dateTime parameter's Kind property is DateTimeKind.Local; otherwise, the method returns false.

Call the TimeZoneInfo.IsDaylightSavingTime method to determine whether to use a time zone's StandardName value or its DaylightName value when displaying the time zone name. See the Example section for an illustration.

See also

Applies to

IsDaylightSavingTime(DateTimeOffset)

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

Indicates whether a specified date and time falls in the range of daylight saving time for the time zone of the current TimeZoneInfo object.

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

Parameters

dateTimeOffset
DateTimeOffset

A date and time value.

Returns

true if the dateTimeOffset parameter is a daylight saving time; otherwise, false.

Remarks

The return value of TimeZoneInfo.IsDaylightSavingTime is affected by the relationship between the time zone represented by the TimeZoneInfo object and the Offset property of the dateTimeOffset parameter. If dateTimeOffset does not correspond to the current time zone's offset from Coordinated Universal Time (UTC), the method converts that time to the time in the current time zone. It then determines whether that date and time is a daylight saving time.

If the time zone represented by the TimeZoneInfo object does not support daylight saving time, the method always returns false. To determine whether a time zone supports daylight saving time, retrieve the value of its SupportsDaylightSavingTime property.

See also

Applies to