C# TimeZoneInfo GetUtcOffset(DateTimeOffset)

Description

TimeZoneInfo GetUtcOffset(DateTimeOffset) 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(DateTimeOffset) has the following syntax.


public TimeSpan GetUtcOffset(
  DateTimeOffset dateTimeOffset
)

Parameters

TimeZoneInfo.GetUtcOffset(DateTimeOffset) has the following parameters.

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

Returns

TimeZoneInfo.GetUtcOffset(DateTimeOffset) method returns An object that indicates the time difference between Coordinated Universal Time (UTC) and the current time zone.

Example


using System;/*w w  w  .j  ava2  s.c om*/

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

        timeoff.ShowOffset(new DateTimeOffset(), TimeZoneInfo.Local);
        timeoff.ShowOffset(DateTimeOffset.UtcNow, TimeZoneInfo.Local);
    }

    private void ShowOffset(DateTimeOffset 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