ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

C# program to get the system time by Zone Id

Here, we are going to learn how to get the system time by Zone Id in C#.Net?
Submitted by Nidhi, on May 07, 2021

Here, we will learn how to get system time by given zone id? For this, we need to use two methods of TimeZoneInfo class that are given below:

  1. FindSystemTimeZoneById()
  2. ConvertTimeFromUtc()

FindSystemTimeZoneById():

This method returns the object of TimeZoneInfo class on the basis of the given zone id.

Syntax:

    TimeZoneInfo TimeZoneInfo.FindSystemTimeZoneById(string zoneId);

Parameter(s):

  • zoneId: Time zone identifier.

Return value:

This method returns the object of TimeZoneInfo.

Exception(s):

  • System.OutOfMemoryException
  • System.ArgumentNullException
  • System.TimeZoneNotFoundException
  • System.Security.SecurityException
  • System.InvalidTimeZoneException

ConvertTimeFromUtc():

This method is used to convert UTC time according to the specified time zone.

Syntax:

    DateTime TimeZoneInfo.ConvertTimeFromUtc(DateTime dt, TimeZoneInfo destTimeZone);

Parameter(s):

  • zoneId: Represents the coordinated Universal Time (UTC)
  • destTimeZone: Represents the time zone to convert

Return value:

It returns date-time according to the specified time zone from UTC time.

Exception(s):

  • System.ArgumentException
  • System.ArgumentNullException

Program:

The source code to get the system time by Zone Id 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()
    {
        DateTime utcTime = DateTime.UtcNow;
        
        //Declared TimeZoneInfo objects.
        TimeZoneInfo IndianStandardZone;
        TimeZoneInfo EasternStandardZone;
        TimeZoneInfo CentralStandardZone;

        //Declared date time objects.
        DateTime IndianStandardTime;
        DateTime EasternStandardTime;
        DateTime CentralStandardTime;

        //Here we get Eastern Standard Time
        EasternStandardZone = TimeZoneInfo.FindSystemTimeZoneById("Eastern Standard Time");
        EasternStandardTime = TimeZoneInfo.ConvertTimeFromUtc(utcTime, EasternStandardZone);
        Console.WriteLine("Eastern Standard Time: "+EasternStandardTime);

        //Here we get India Standard Time
        IndianStandardZone = TimeZoneInfo.FindSystemTimeZoneById("India Standard Time");
        IndianStandardTime = TimeZoneInfo.ConvertTimeFromUtc(utcTime, IndianStandardZone);
        Console.WriteLine("India Standard Time: "+IndianStandardTime);

        //Here we get Central Standard Time
        CentralStandardZone = TimeZoneInfo.FindSystemTimeZoneById("Central Standard Time");
        CentralStandardTime = TimeZoneInfo.ConvertTimeFromUtc(utcTime, CentralStandardZone);
        Console.WriteLine("Central Standard Time: "+CentralStandardTime);
    }
}

Output:

Eastern Standard Time: 2/4/2020 11:23:52 AM
India Standard Time: 2/4/2020 9:53:52 PM
Central Standard Time: 2/4/2020 10:23:52 AM
Press any key to continue . . .

C# TimeZoneInfo Class Programs »

ADVERTISEMENT




Comments and Discussions

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

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.