TimeZoneInfo.IsDaylightSavingTime Method (DateTime)

May 11, 2014

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)

public bool IsDaylightSavingTime(
	DateTime dateTime
)

Parameters

dateTime
Type: System.DateTime
A date and time value.

Return Value

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

ExceptionCondition
ArgumentException

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

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.


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.

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(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";
}


Windows Phone OS

Supported in: 8.1, 8.0, 7.1, 7.0

Windows Phone

Show:
© 2014 Microsoft