Q:

C# program to get the system time by Zone Id

belongs to collection: C# TimeZoneInfo Class Programs

0

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
  •  

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

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 . . .

 

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

C# TimeZoneInfo Class Programs

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
C# program to convert a time to the time in a part... >>
<< C# program to get the Coordinated Universal Time (...