Check if a date and time(DateTimeOffset) is daylight saving time for time zone from TimeZoneInfo in CSharp

Description

The following code shows how to check if a date and time(DateTimeOffset) is daylight saving time for time zone from TimeZoneInfo.

Example


//from   w  w  w  .  j  a v a 2s  . c om

using System;
public class MainClass
{
    public static void Main(String[] argv)
    {
        DateTimeOffset unclearDate = DateTimeOffset.Now;

        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 ");

        if (TimeZoneInfo.Local.IsAmbiguousTime(unclearDate) ||
            TimeZoneInfo.Local.IsDaylightSavingTime(unclearDate))
            Console.WriteLine("{0} may be daylight saving time in {1}.",
                              unclearDate, TimeZoneInfo.Local.DisplayName);

    }
}

The code above generates the following result.





















Home »
  C# Tutorial »
    Data Types »




C# Data Types
Bool
Byte
Char
Decimal
Double
Float
Integer
Long
Short
String
C# Array
Array Example
Byte Array
C# Standard Data Type Format
BigInteger
Complex
Currency
DateTime
DateTimeOffset
DateTime Format Parse Convert
TimeSpan
TimeZone
Enum
Null
tuple
var