Click here to Skip to main content
15,860,859 members
Articles / Programming Languages / Visual Basic
Article

Time Zone Class

Rate me:
Please Sign up or sign in to vote.
4.67/5 (17 votes)
20 Sep 2007CPOL3 min read 143.8K   4.5K   52   28
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

NamePurpose
SetTimeZoneInformation To set the current system time zone

Properties

NameDescription
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

NameDescription Parameter Return Value
GetTimeZones Gets an array of all time zones on the system NoneArray 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 NoneTrue/False (Boolean)
Refresh Refreshes the information of the time zone object NoneNone
SortSorts 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 noneDisplay Name (String)
Equals Determines whether the specified System.Object is equal to the current System.Objectobj (Object)True/False (Boolean)

Using The Code

For more examples, check the demo project.

VB.NET
'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

History

  • 12 September, 2007 -- Original version posted
  • 17 September, 2007 -- (Some internal minor changes to optimize the Sort functionality)

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior) ZipEdTech
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionIt's C# version Pin
aree21427-Nov-14 18:47
aree21427-Nov-14 18:47 
QuestionThe system don't Allow me to change time Zone Pin
jason55724-Aug-12 7:01
jason55724-Aug-12 7:01 
GeneralMy vote of 5 Pin
Manoj Kumar Choubey23-Feb-12 21:53
professionalManoj Kumar Choubey23-Feb-12 21:53 
GeneralConverting TimeZone Values Pin
cheecio17-Feb-10 1:12
cheecio17-Feb-10 1:12 
GeneralWindows Vista and Windows 7 Pin
Nonasm9-Feb-10 21:23
Nonasm9-Feb-10 21:23 
GeneralNot running in ASP.NET Pin
Xavito11-Jun-09 9:50
Xavito11-Jun-09 9:50 
GeneralRe: Not running in ASP.NET Pin
Arman Ghazanchyan11-Jun-09 10:24
Arman Ghazanchyan11-Jun-09 10:24 
GeneralRe: Not running in ASP.NET Pin
Xavito11-Jun-09 16:03
Xavito11-Jun-09 16:03 
GeneralRe: Not running in ASP.NET Pin
Xavito11-Jun-09 16:12
Xavito11-Jun-09 16:12 
GeneralRe: Not running in ASP.NET Pin
Arman Ghazanchyan9-Aug-09 19:32
Arman Ghazanchyan9-Aug-09 19:32 
GeneralRe: Not running in ASP.NET [modified] Pin
Fundacion8-May-10 9:46
Fundacion8-May-10 9:46 
GeneralOS Localization Compatibility Pin
simster7723-Jan-09 10:01
simster7723-Jan-09 10:01 
GeneralRe: OS Localization Compatibility Pin
Arman Ghazanchyan23-Jan-09 10:43
Arman Ghazanchyan23-Jan-09 10:43 
GeneralRe: OS Localization Compatibility Pin
simster7723-Jan-09 10:47
simster7723-Jan-09 10:47 
GeneralRe: OS Localization Compatibility Pin
EngBol3-Nov-11 6:25
EngBol3-Nov-11 6:25 
GeneralNew approach in .Net 3.5 Pin
neilio2-Dec-08 8:59
neilio2-Dec-08 8:59 
GeneralRe: New approach in .Net 3.5 Pin
Arman Ghazanchyan3-Dec-08 11:56
Arman Ghazanchyan3-Dec-08 11:56 
GeneralHelp Required Pin
Krishnraj16-Sep-08 1:11
Krishnraj16-Sep-08 1:11 
GeneralHELP PLEASE Pin
samiji15-Sep-08 20:04
samiji15-Sep-08 20:04 
GeneralRe: HELP PLEASE [modified] Pin
Arman Ghazanchyan15-Sep-08 20:24
Arman Ghazanchyan15-Sep-08 20:24 
GeneralGood, but don't depend on the registry Pin
neilio25-Sep-07 17:02
neilio25-Sep-07 17:02 
GeneralRe: Good, but don't depend on the registry [modified] Pin
Arman Ghazanchyan25-Sep-07 18:30
Arman Ghazanchyan25-Sep-07 18:30 
GeneralRe: Good, but don't depend on the registry Pin
iwonder26-Sep-07 5:22
iwonder26-Sep-07 5:22 
GeneralRe: Good, but don't depend on the registry Pin
Arman Ghazanchyan26-Sep-07 8:27
Arman Ghazanchyan26-Sep-07 8:27 
GeneralRe: Good, but don't depend on the registry Pin
iwonder27-Sep-07 6:48
iwonder27-Sep-07 6:48 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.