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?
By Nidhi Last updated : March 31, 2023

Getting the system time by Zone Id

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()

1) Using 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

2) Using 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

C# code to get the system time by Zone Id

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 »





Comments and Discussions!

Load comments ↻





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