C# - TimeZoneInfo.CreateCustomTimeZone() Method with Example

In this tutorial, we will learn about the C# TimeZoneInfo.CreateCustomTimeZone() method with its definition, usage, syntax, and example. By Nidhi Last updated : March 31, 2023

TimeZoneInfo.CreateCustomTimeZone() Method

The TimeZoneInfo.CreateCustomTimeZone() 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

C# Example of TimeZoneInfo.CreateCustomTimeZone() Method

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 »





Comments and Discussions!

Load comments ↻





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