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.