C# - TimeZoneInfo.GetUtcOffset() Method with Example

In this tutorial, we will learn about the C# TimeZoneInfo.GetUtcOffset() method with its definition, usage, syntax, and example. By Nidhi Last updated : March 31, 2023

TimeZoneInfo.GetUtcOffset() Method

The TimeZoneInfo.GetUtcOffset() is used to calculate the offset between the time in this time zone and Coordinated Universal Time (UTC) for a particular date and time.

Syntax

TimeSpan TimeZoneInfo.GetUtcOffset(DateTime date_time);

Parameter(s)

  • date_time: date_time to find offset from UTC time.

Return Value

This method returns the object of TimeSpan that represents offset from UTC.

C# Example of TimeZoneInfo.GetUtcOffset() Method

The source code to calculate the offset between the time in this time zone and UTC is given below. The given program is compiled and executed successfully.

using System;
using System.Globalization;
using System.Collections.ObjectModel;

class TimeZoneInfoDemo {
  //Entry point of Program
  static public void Main() {
    TimeZoneInfo cst;
    TimeZoneInfo utc;

    DateTime time;
    TimeSpan offset;

    time = new DateTime(2020, 1, 1, 12, 30, 30);
    cst = TimeZoneInfo.FindSystemTimeZoneById("Central Standard Time");
    utc = TimeZoneInfo.Utc;

    offset = utc.GetUtcOffset(time);
    Console.WriteLine("UTC Time Offset: " + offset);

    offset = cst.GetUtcOffset(time);
    Console.WriteLine("CST Time Offset: " + offset);

  }
}

Output

UTC Time Offset: 00:00:00
CST Time Offset: -06:00:00
Press any key to continue . . .

C# TimeZoneInfo Class Programs »





Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.