Time Zone Class






4.67/5 (16 votes)
The TimeZoneInfo class provides properties and methods for any time zone on the system.
Introduction
This is yet another time zone class that gives you full access and functionality for different time zones. As we all know, .NET's (up to Framework 2) Time Zone class is not of much use since it only gives us objects that are associated with the current time zone. Therefore, I decided to create a time zone class called TimeZoneInfo
that can give us information and methods for the other time zones.
The TimeZoneInfo
class does pure .NET implementation and uses only one API method to set the current system computer's TimeZone
. This means 99% managed code compared to some other time zone implementations.
Background
I have seen many time zone classes, but almost all of them were lacking some features or the functions were implemented incorrectly. Time zone class may seem to be an easy task at first, but believe me, it is kind of tricky. The TimeZoneInfo
class is based on the windows registry information under the key HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\Time Zones, so it is critical that the registry includes the latest updates from Microsoft.
The class provides all the information and the functionality for the time zones. For example, it provides Daylight Saving Time dates for the time zone, Current UtcOffset, Current Time, Current System Time Zone etc.
Used API Functions
Name | Purpose |
SetTimeZoneInformation |
To set the current system time zone |
Properties
Name | Description |
DisplayName |
Gets the display name of the time zone |
DaylightName |
Gets the daylight saving name of the time zone |
StandardName |
Gets the standard name of the time zone |
CurrentTime |
Gets the current date and time of the time zone |
CurrentUtcOffset |
Gets the current UTC (Coordinated Universal Time) offset of the time zone |
CurrentTimeZone |
Gets or sets the current time zone for this computer system |
StandardUtcOffset |
Gets the standard UTC (Coordinated Universal Time) offset of the time zone |
Id |
Gets the id of the time zone |
Methods
Name | Description | Parameter | Return Value |
GetTimeZones |
Gets an array of all time zones on the system | None | Array of TimeZoneInfo |
FromStandardName |
Gets a TimeZoneInfo.Object from standard name |
Standard Name (string ) |
TimeZoneInfo |
FromId |
Gets a TimeZoneInfo.Object from Id |
Id (string) |
TimeZoneInfo |
GetDaylightChanges |
Returns the daylight saving time for a particular year | Year (Integer) |
DaylightTime |
IsDaylightSavingTime |
Returns a value indicating whether this time zone is within a daylight saving time period | None | True /False (Boolean ) |
Refresh |
Refreshes the information of the time zone object | None | None |
Sort |
Sorts the elements in a list(Of TimeZoneInfo ) object/TimeZoneInfo array based on standard UTC offset or display name |
tzInfos (List(Of TimeZoneInfo )/TimeZoneInfo array |
None |
ToString |
Returns a System.String that represents the current TimeZoneInfo object |
none | Display Name (String ) |
Equals |
Determines whether the specified System.Object is equal to the current System.Object |
obj (Object) |
True /False (Boolean ) |
Using The Code
For more examples, check the demo project.
'Get all the time zones on the system and show them in a ListBox control
Me.ListBox1.DataSource = TimeZoneInfo.GetTimeZones
Dim tzInfo As TimeZoneInfo
'Create new instance of a TimeZoneInfo for a specific time zone
tzInfo = New TimeZoneInfo("Pacific Standard Time")
'Or tzInfo = TimeZoneInfo.FromStandardName("Pacific Standard Time")
'Get the current UtcOffset of the time zone
Dim utcOffset As TimeSpan = tzInfo.CurrentUtcOffset
'Get the current time of the time zone
Dim time As DateTime = tzInfo.CurrentTime
'Get a value specifying if it is a daylight saving time
'currently for the time zone
Dim isDaylight As Boolean = tzInfo.IsDaylightSavingTime
'Get daylight changes for the year 2007 of the time zone
Dim dStart, dEnd As DateTime
Dim dt As System.Globalization.DaylightTime
dt = tzInfo.GetDaylightChanges(2007)
dStart = dt.Start
dEnd = dt.End
'And much more
'...
'...
System Requirements
- Windows NT/2000/XP (tested only on XP)
- Framework 2.0
TimeZoneInfo Information
- Author name: Arman Ghazanchyan
- External link to TimeZoneInfo class
History
- 12 September, 2007 -- Original version posted
- 17 September, 2007 -- (Some internal minor changes to optimize the
Sort
functionality)