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

Description

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

Example


using System;/*from  w w w . j  ava 2s. c om*/

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

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

    private static 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 »
    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