C# program to define a time zone that is not found on the local computer (TimeZoneInfo.CreateCustomTimeZone() Method)

C# TimeZoneInfo.CreateCustomTimeZone() Method: Here, we are going to learn how to define a time zone that is not found on the local computer in C#.Net?
Submitted by Nidhi, on May 19, 2021

The CreateCustomTimeZone() method of TimeZoneInfo class is a static method and used to create our time zone using time offset, standard name, and display name.

Syntax:

    TimeZoneInfo TimeZoneInfo.CreateCustomTimeZone(
        string id, 
        TimeSpan baseUtcOffset, 
        string displayName, 
        string standardname);

Parameter(s):

  • id: Unique id to create custom time-zone.
  • baseUtcOffset: Time offset for custom time zone.
  • displayName: Display name for custom time zone.
  • standardname: Standard name for custom time zone.

Return value:

It returns an object of a custom time zone.

Exception(s):

  • System.ArgumentException
  • System.ArgumentNullException
  • System.ArgumentOutOfRangeException

Program:

The source code to define a time zone that is not found on the local computer 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()
    {
        string displayName;
        string standardZoneName;
        TimeSpan timeOffset;
        TimeZoneInfo customZone;

        timeOffset = new TimeSpan(05, 00, 00);
        standardZoneName = "Delhi Time";
        displayName = "(GMT+05:00) Delhi/New-Delhi Time";

        customZone = TimeZoneInfo.CreateCustomTimeZone(standardZoneName, timeOffset, displayName, standardZoneName);
        
        Console.WriteLine("Current time:  "+TimeZoneInfo.ConvertTime(DateTime.Now, TimeZoneInfo.Local, customZone));
        Console.WriteLine("Standard Name: "+customZone.StandardName);
        Console.WriteLine("Display Name:  "+customZone.DisplayName);

    }
}

Output:

Current time:  2/9/2020 2:21:36 PM
Standard Name: Delhi Time
Display Name:  (GMT+05:00) Delhi/New-Delhi Time
Press any key to continue . . .

C# TimeZoneInfo Class Programs »


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