CSharp - DateTime and Daylight Saving Time

Introduction

IsDaylightSavingTime tells you whether a given local DateTime is subject to daylight saving time. UTC times always return false:

Demo

using System;
class MainClass//from   w w w  . j  a v a 2s  . c  o  m
{
   public static void Main(string[] args)
   {
     Console.Write (DateTime.Now.IsDaylightSavingTime());     // True or False
     Console.Write (DateTime.UtcNow.IsDaylightSavingTime());  // Always False
   }
}

Result

The following code shows how to deal with time being moved forward during daylight saving time.

The code instantiates a DateTime right in the time changing zone.

Demo

using System;
using System.Globalization;

class MainClass/*from  w  w  w . j av  a2 s.  c  om*/
{
    public static void Main(string[] args)
    {
        DaylightTime changes = TimeZone.CurrentTimeZone.GetDaylightChanges(2010);
        TimeSpan halfDelta = new TimeSpan(changes.Delta.Ticks / 2);
        DateTime utc1 = changes.End.ToUniversalTime() - halfDelta;
        DateTime utc2 = utc1 - changes.Delta;

        DateTime loc1 = utc1.ToLocalTime();
        DateTime loc2 = utc2.ToLocalTime();
        Console.WriteLine(loc1);
        Console.WriteLine(loc2);
        Console.WriteLine(loc1 == loc2);

        Console.Write(loc1.ToString("o"));
        Console.Write(loc2.ToString("o"));

        Console.WriteLine(loc1.ToUniversalTime() == utc1);
        Console.WriteLine(loc2.ToUniversalTime() == utc2);
    }
}

Result