次の方法で共有


TimeZoneInfo.IsDaylightSavingTime Method (DateTime)

Microsoft Silverlight will reach end of support after October 2021. Learn more.

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

Namespace:  System
Assembly:  mscorlib (in mscorlib.dll)

Syntax

'Declaration
Public Function IsDaylightSavingTime ( _
    dateTime As DateTime _
) As Boolean
public bool IsDaylightSavingTime(
    DateTime dateTime
)

Parameters

Return Value

Type: System.Boolean
true if the dateTime parameter is a daylight saving time; otherwise, false.

Exceptions

Exception Condition
ArgumentException

The Kind property of the dateTime value is DateTimeKind.Local and dateTime is an invalid 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 TimeZoneInfoobject.

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

DateTimeKind.Utc

Converts UTC to the equivalent time of the TimeZoneInfo object and then determines whether the latter is daylight saving time.

Any other TimeZoneInfoobject.

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.

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.

Dim unclearDate As Date = #11/4/2007 1:30AM#
' Test if time is ambiguous.
outputBlock.Text += String.Format("In the {0}, {1} is {2}ambiguous.", _ 
                  TimeZoneInfo.Local.DisplayName, _
                  unclearDate, _
                  IIf(TimeZoneInfo.Local.IsAmbiguousTime(unclearDate), "", "not ")) _
                  + vbCrLf
' Test if time is DST.
outputBlock.Text += String.Format("In the {0}, {1} is {2}daylight saving time.", _ 
                  TimeZoneInfo.Local.DisplayName, _
                  unclearDate, _
                  IIf(TimeZoneInfo.Local.IsDaylightSavingTime(unclearDate), "", "not ")) _
                  + vbCrLf
outputBlock.Text += vbCrLf    
' Report time as DST if it is either ambiguous or DST.
If TimeZoneInfo.Local.IsAmbiguousTime(unclearDate) OrElse _ 
   TimeZoneInfo.Local.IsDaylightSavingTime(unclearDate) Then
    outputBlock.Text += String.Format("{0} may be daylight saving time in {1}.", _ 
                      unclearDate, TimeZoneInfo.Local.DisplayName) + vbCrLf                                           
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 unclearDate = new DateTime(2007, 11, 4, 1, 30, 0);
// Test if time is ambiguous.
outputBlock.Text += String.Format("In the {0}, {1} is {2}ambiguous.\n", 
                  TimeZoneInfo.Local.DisplayName, 
                  unclearDate, 
                  TimeZoneInfo.Local.IsAmbiguousTime(unclearDate) ? "" : "not ");
// Test if time is DST.
outputBlock.Text += String.Format("In the {0}, {1} is {2}daylight saving time.\n", 
                  TimeZoneInfo.Local.DisplayName, 
                  unclearDate, 
                  TimeZoneInfo.Local.IsDaylightSavingTime(unclearDate) ? "" : "not ");
outputBlock.Text += "\n";    
// Report time as DST if it is either ambiguous or DST.
if (TimeZoneInfo.Local.IsAmbiguousTime(unclearDate) || 
    TimeZoneInfo.Local.IsDaylightSavingTime(unclearDate))
    outputBlock.Text += String.Format("{0} may be daylight saving time in {1}.\n", 
                      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).

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.

Version Notes

XNA Framework

 When this method is used in the XNA Framework, it throws a NotSupportedException exception.

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 Sub DisplayDateWithTimeZoneName(ByVal outputBlock As System.Windows.Controls.TextBlock, ByVal date1 As Date, ByVal timeZone As TimeZoneInfo)
   outputBlock.Text += String.Format("The time is {0:t} on {0:d} {1}", _
                     date1, _
                     IIf(timeZone.IsDaylightSavingTime(date1), _
                         timeZone.DaylightName, timeZone.StandardName)) + vbCrLf
End Sub
private void DisplayDateWithTimeZoneName(System.Windows.Controls.TextBlock outputBlock, 
                                         DateTime date1, TimeZoneInfo timeZone)
{
   outputBlock.Text += String.Format("The time is {0:t} on {0:d} {1}",
                     date1,
                     timeZone.IsDaylightSavingTime(date1) ?
                         timeZone.DaylightName : timeZone.StandardName) + "\n";
}

Version Information

Silverlight

Supported in: 5, 4, 3

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0

XNA Framework

Supported in: Xbox 360, Windows Phone OS 7.0

Platforms

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.