C# program to calculate the offset between the time in this time zone and UTC (TimeZoneInfo.GetUtcOffset() Method)

C# TimeZoneInfo.GetUtcOffset() Method: Here, we are going to learn how to calculate the offset between the time in this time zone and UTC in C#.Net?
Submitted by Nidhi, on May 20, 2021

The GetUtcOffset() method of TimeZoneInfo class 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.

Program:

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 »


ADVERTISEMENT
ADVERTISEMENT




Comments and Discussions!




Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.