CSharp - DateTimeOffset and Time Zones


DateTimeOffset instance value comparisons honor the time zone difference.

ToUniversalTime/ToLocalTime methods return a DateTimeOffset representing the same point in time, but with a UTC or local offset.


using System;
class MainClass/* w ww .j  a v a2s.c om*/
   public static void Main(string[] args)

     DateTimeOffset local = DateTimeOffset.Now;
     DateTimeOffset utc   = local.ToUniversalTime();

     Console.WriteLine (local.Offset);  
     Console.WriteLine (utc.Offset);    

     Console.WriteLine (local == utc);  

     //To include the Offset in the comparison, you must use the  EqualsExact method:

     Console.WriteLine (local.EqualsExact (utc));