Click here to Skip to main content
Click here to Skip to main content

Time Zone Class

, 20 Sep 2007
Rate this:
Please Sign up or sign in to vote.
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

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)

About the Author

VBDT
Software Developer (Senior)
United States United States
No Biography provided

Comments and Discussions

 
QuestionThe system don't Allow me to change time Zone Pinmemberjason55724-Aug-12 7:01 
GeneralMy vote of 5 Pinmembermanoj kumar choubey23-Feb-12 21:53 
GeneralConverting TimeZone Values Pinmembercheecio17-Feb-10 1:12 
GeneralWindows Vista and Windows 7 PinmemberNonasm9-Feb-10 21:23 
GeneralNot running in ASP.NET PinmemberXavito11-Jun-09 9:50 
GeneralRe: Not running in ASP.NET PinmemberVBDT11-Jun-09 10:24 
GeneralRe: Not running in ASP.NET PinmemberXavito11-Jun-09 16:03 
GeneralRe: Not running in ASP.NET PinmemberXavito11-Jun-09 16:12 
GeneralRe: Not running in ASP.NET PinmemberVBDT9-Aug-09 19:32 
GeneralRe: Not running in ASP.NET [modified] PinmemberFundacion8-May-10 9:46 
GeneralOS Localization Compatibility Pinmembersimster7723-Jan-09 10:01 
GeneralRe: OS Localization Compatibility PinmemberVBDT23-Jan-09 10:43 
GeneralRe: OS Localization Compatibility Pinmembersimster7723-Jan-09 10:47 
GeneralRe: OS Localization Compatibility PinmemberEngBol3-Nov-11 6:25 
GeneralNew approach in .Net 3.5 Pinmemberneilio2-Dec-08 8:59 
GeneralRe: New approach in .Net 3.5 PinmemberVBDT3-Dec-08 11:56 
GeneralHelp Required PinmemberKrishnraj16-Sep-08 1:11 
GeneralHELP PLEASE Pinmembersamiji15-Sep-08 20:04 
GeneralRe: HELP PLEASE [modified] PinmemberVBDT15-Sep-08 20:24 
GeneralGood, but don't depend on the registry Pinmemberneilio25-Sep-07 17:02 
GeneralRe: Good, but don't depend on the registry [modified] PinmemberVBDT25-Sep-07 18:30 
GeneralRe: Good, but don't depend on the registry Pinmemberiwonder26-Sep-07 5:22 
GeneralRe: Good, but don't depend on the registry PinmemberVBDT26-Sep-07 8:27 
GeneralRe: Good, but don't depend on the registry Pinmemberiwonder27-Sep-07 6:48 
GeneralRe: Good, but don't depend on the registry [modified] PinmemberVBDT27-Sep-07 9:39 

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

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

| Advertise | Privacy | Mobile
Web03 | 2.8.140709.1 | Last Updated 21 Sep 2007
Article Copyright 2007 by VBDT
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid