Export (0) Print
Expand All

TimeZoneInfo.IsDaylightSavingTime Method (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.

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

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

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.

Notes to Callers

On Windows XP systems, this method applies only the current adjustment rule if the current instance is TimeZoneInfo.Local. As a result, the method may not accurately report whether dateTime is a daylight saving time for periods before the current adjustment rule came into effect. For more information, see the Notes for Callers section in the Local property.

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   

.NET Framework

Supported in: 4.5, 4, 3.5

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Portable Class Library

Supported in: Portable Class Library

.NET for Windows Store apps

Supported in: Windows 8

.NET for Windows Phone apps

Supported in: Windows Phone 8.1, Windows Phone Silverlight 8.1, Windows Phone Silverlight 8

Windows Phone 8.1, Windows Phone 8, Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Show:
© 2014 Microsoft