CSharp - DateTime and Time Zones

Introduction

DateTime handles time zone by its DateTimeKind property which as the following three values:Unspecified, Local, or Utc.

When you compare two DateTime instances, only their date time values are compared, and DateTimeKinds are ignored:

Demo

using System;
class MainClass/* ww  w.  j  ava2  s.  c o  m*/
{
   public static void Main(string[] args)
   {
         DateTime dt1 = new DateTime (2000, 1, 1, 10, 20, 30, DateTimeKind.Local);
         DateTime dt2 = new DateTime (2000, 1, 1, 10, 20, 30, DateTimeKind.Utc);
         Console.WriteLine (dt1 == dt2);        
         DateTime local = DateTime.Now;
         DateTime utc = local.ToUniversalTime();
         Console.WriteLine (local == utc);      

   }
}

Result

ToUniversalTime/ToLocalTime convert date time value to universal/local time.

These method use the computer's current time zone and return a new Date Time with a DateTimeKind of Utc or Local.

No conversion happens if you call ToUniversalTime on a DateTime that's already Utc, or ToLocalTime on a DateTime that's already Local.

You will get a conversion if you call ToUniversalTime or ToLocalTime on a DateTime that's Unspecified.

You can construct a DateTime that differs from another only in Kind with the static DateTime.SpecifyKind method:

Demo

using System;
class MainClass/*from   ww  w  . ja v a  2s . com*/
{
   public static void Main(string[] args)
   {
         DateTime d = new DateTime (2015, 12, 12);  // Unspecified
         DateTime utc = DateTime.SpecifyKind (d, DateTimeKind.Utc);
         Console.WriteLine (utc);            // 12/12/2015 12:00:00 AM
   }
}

Result