Find offset or difference between current time zone and Coordinated Universal Time (UTC) in CSharp

Description

The following code shows how to find offset or difference between current time zone and Coordinated Universal Time (UTC).

Example


using System;/*from  w  ww.  j  a va  2  s .  c o m*/

   public class TimeOffsets
   {
      public static void Main()
      {
         TimeOffsets timeoff = new TimeOffsets();
         TimeZoneInfo cst = TimeZoneInfo.FindSystemTimeZoneById("Central Standard Time");

         timeoff.ShowOffset(new DateTime(2006, 6, 12, 11, 0, 0), TimeZoneInfo.Local);
         timeoff.ShowOffset(DateTime.UtcNow, TimeZoneInfo.Local);
         timeoff.ShowOffset(new DateTime(2006, 6, 12, 11, 0, 0), TimeZoneInfo.Utc);
         timeoff.ShowOffset(DateTime.Now, TimeZoneInfo.Utc);
         timeoff.ShowOffset(new DateTime(2006, 6, 12, 11, 0, 0), cst);
         timeoff.ShowOffset(new DateTime(2007, 11, 14, 00, 00, 00, DateTimeKind.Local), cst);
      }

      private void ShowOffset(DateTime time, TimeZoneInfo timeZone)
      { 
         TimeSpan offset;
         offset = timeZone.GetUtcOffset(time);
         Console.WriteLine(offset);                                             
      }
   }

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