C# TimeZoneInfo GetUtcOffset(DateTime)

Description

TimeZoneInfo GetUtcOffset(DateTime) calculates the offset or difference between the time in this time zone and Coordinated Universal Time (UTC) for a particular date and time.

Syntax

TimeZoneInfo.GetUtcOffset(DateTime) has the following syntax.


public TimeSpan GetUtcOffset(
  DateTime dateTime
)

Parameters

TimeZoneInfo.GetUtcOffset(DateTime) has the following parameters.

  • dateTime - The date and time to determine the offset for.

Returns

TimeZoneInfo.GetUtcOffset(DateTime) method returns An object that indicates the time difference between the two time zones.

Example

The following example illustrates the use of the GetUtcOffset(DateTime) method with different time zones and with date values that have different Kind property values.


using System;/*from  ww w . j av a  2s. 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 »
    System »




Array
BitConverter
Boolean
Byte
Char
Console
ConsoleKeyInfo
Convert
DateTime
DateTimeOffset
Decimal
Double
Enum
Environment
Exception
Guid
Int16
Int32
Int64
Math
OperatingSystem
Random
SByte
Single
String
StringComparer
TimeSpan
TimeZone
TimeZoneInfo
Tuple
Tuple
Tuple
Type
UInt16
UInt32
UInt64
Uri
Version