C# TimeZoneInfo CreateCustomTimeZone(String, TimeSpan, String, String, String, TimeZoneInfo.AdjustmentRule[])

Description

TimeZoneInfo CreateCustomTimeZone(String, TimeSpan, String, String, String, TimeZoneInfo.AdjustmentRule[]) creates a custom time zone with a specified identifier, an offset from Coordinated Universal Time (UTC), a display name, a standard time name, a daylight saving time name, and daylight saving time rules.

Syntax

TimeZoneInfo.CreateCustomTimeZone(String, TimeSpan, String, String, String, TimeZoneInfo.AdjustmentRule[]) has the following syntax.


public static TimeZoneInfo CreateCustomTimeZone(
  string id,//from   w  w w . j a  v  a  2  s .c  o  m
  TimeSpan baseUtcOffset,
  string displayName,
  string standardDisplayName,
  string daylightDisplayName,
  TimeZoneInfo.AdjustmentRule[] adjustmentRules
)

Parameters

TimeZoneInfo.CreateCustomTimeZone(String, TimeSpan, String, String, String, TimeZoneInfo.AdjustmentRule[]) has the following parameters.

  • id - The time zone's identifier.
  • baseUtcOffset - An object that represents the time difference between this time zone and Coordinated Universal Time (UTC).
  • displayName - The display name of the new time zone.
  • standardDisplayName - The new time zone's standard time name.
  • daylightDisplayName - The daylight saving time name of the new time zone.
  • adjustmentRules - An array that augments the base UTC offset for a particular period.

Returns

TimeZoneInfo.CreateCustomTimeZone(String, TimeSpan, String, String, String, TimeZoneInfo.AdjustmentRule[]) method returns A TimeZoneInfo object that represents the new time zone.

Example

The following example creates a custom time zone.


/* ww  w.  ja  va 2  s.  c om*/
using System;
public class MainClass{
  public static void Main(String[] argv){  
    TimeZoneInfo.TransitionTime startTransition, endTransition;
    startTransition = TimeZoneInfo.TransitionTime.CreateFloatingDateRule(new DateTime(1, 1, 1, 4, 0, 0), 
                                                                      10, 2, DayOfWeek.Sunday); 
    endTransition = TimeZoneInfo.TransitionTime.CreateFloatingDateRule(new DateTime(1, 1, 1, 3, 0, 0), 
                                                                    3, 2, DayOfWeek.Sunday);
    // Define adjustment rule
    TimeSpan delta = new TimeSpan(1, 0, 0);
    TimeZoneInfo.AdjustmentRule adjustment;
    adjustment = TimeZoneInfo.AdjustmentRule.CreateAdjustmentRule(new DateTime(1999, 10, 1), DateTime.MaxValue.Date, delta, startTransition, endTransition);
    
    TimeZoneInfo.AdjustmentRule[] adjustments = {adjustment};
    
    string displayName = "(GMT-04:00) Custom Time";
    string standardName = "Palmer Time";
    string daylightName = "Palmer Daylight Time";
    TimeSpan offset = new TimeSpan(-4, 0, 0);
    TimeZoneInfo palmer = TimeZoneInfo.CreateCustomTimeZone(standardName, offset, displayName, standardName, daylightName, adjustments);
    Console.WriteLine("The current time is {0} {1}",  
                      TimeZoneInfo.ConvertTime(DateTime.Now, TimeZoneInfo.Local, palmer), 
                      palmer.StandardName);

  }
}
    

The code above generates the following result.





















Home »
  C# Tutorial »
    System »




Array
BitConverter
Boolean
Byte
Char
Console
ConsoleKeyInfo
Convert
DateTime
DateTimeOffset
Decimal
Double
Enum
Environment
Exception
Guid
Int16
Int32
Int64
Math
OperatingSystem
Random
SByte
Single
String
StringComparer
TimeSpan
TimeZone
TimeZoneInfo
Tuple
Tuple
Tuple
Type
UInt16
UInt32
UInt64
Uri
Version