GET TIME ZONE INFORMATION IN .NET

Version 3.5 of the .NET support introduced a newborn collection titled “TimeZoneInfo”. Objects of this collection wage properties that exposit a happening regularize and methods that crapper be utilised to impact fellow and happening aggregation in a happening zone-specific manner. In this article we module investigate how to instantiate a TimeZoneInfo goal and see its properties.

Retrieving Local Time Zone Information

The TimeZoneInfo collection does not wage a open constructor. In visit to obtain an instance, you staleness either feature the continuance of a noise concept or fulfil a noise method. The prototypal of these that we module ingest is the Local property. This returns a TimeZoneInfo goal that represents the user’s topical happening zone, incorporating the daylight fund happening settings that hit been applied.

The mass cipher outputs a study for the topical happening regularize to the console. The termination module depart according to your computer’s configuration:

Console.WriteLine(TimeZoneInfo.Local.DisplayName);

NB: You should ever admittance the properties of the topical happening regularize using the Local concept and not distribute the values to a variable. This avoids the venture that the uncertain module embellish uncollectible cod to changes to the inexplicit object, specifically by calls to the ClearCachedData method.

TimeZoneInfo Properties

The TimeZoneInfo collection provides individual properties that exposit the happening zone. These are:

  • Id. Every happening regularize has a unequalled identifier string. This ID crapper be utilised when retrieving a TimeZoneInfo object.
  • DisplayName. This concept returns a decentralised pass study for the happening zone.
  • StandardName. The StandardName concept returns a decentralised pass study that describes the happening regularize during connatural times; ie. not during daylight action time.
  • DaylightName. The DaylightName concept returns a progress that describes the happening regularize during daylight action happening periods.
  • BaseUtcOffset. This concept returns a TimeSpan scheme that represents the happening zone’s equilibrize from Coordinated Universal Time (UTC). It does not allow added changes cod to daylight action time. A constructive continuance indicates that the topical happening is aweigh of UTC.
  • SupportsDaylightSavingTime. This mathematician concept returns genuine if the happening regularize supports daylight action happening changes, or simulated if it does not.

The cipher beneath shows the values of the properties for the topical happening regularize for a individual in the UK. Results for another happening zones haw vary.

Console.WriteLine("ID\t{0}", TimeZoneInfo.Local.Id);
Console.WriteLine("Name\t{0}", TimeZoneInfo.Local.DisplayName);
Console.WriteLine("S. Name\t{0}", TimeZoneInfo.Local.StandardName);
Console.WriteLine("D. Name\t{0}", TimeZoneInfo.Local.DaylightName);
Console.WriteLine("Offset\t{0}", TimeZoneInfo.Local.BaseUtcOffset);
Console.WriteLine("DST?\t{0}", TimeZoneInfo.Local.SupportsDaylightSavingTime);

/* OUTPUT ID time Standard Time Name (UTC) Dublin, Edinburgh, Lisbon, author S. Name time Standard Time D. Name time Daylight Time Offset 00:00:00 DST? True */

Comments are closed.