Click here to Skip to main content
Licence CPOL
First Posted 6 Jun 2008
Views 19,854
Downloads 154
Bookmarked 21 times

How to use time-zones in .NET under Windows

By | 6 Jun 2008 | Article
This article describes how to convert between arbitrary time-zones in .NET 2.0.

Introduction

Time-zone handling in .NET 2.0 is restricted to converting between UTC and local (usually server) time. However, in order to display DateTime correctly to users in different regions, in particular, in web applications, you need to be able to convert DateTime between UTC (which is what you should always store in your DB) and the target time zone.

Background

The source code includes a library (basically a single class) and a sample application that shows how to use the library.

The library reads the registry for time-zone names and basic information, and uses Interop to access the Windows system calls necessary to convert between the different time zones.

Using the code

The following lines are taken from the sample application:

// list all time zones
TimeZoneInformation[] arrTzi = TimeZoneInformation.EnumZones();
foreach (TimeZoneInformation tzi in arrTzi)
{
    Console.Write("name: " + tzi.Name);
    Console.WriteLine(" --- now: " + tzi.FromUniversalTime(dt));
}

Each instance of the TimeZoneInformation class has the following useful attributes: Name, DisplayName, Index, Bias (against UTC), DaylightBias, DaylightName, StandardBias, and StandardName.

Converting between time-zones is as easy as this:

TimeZoneInformation tziLocal = TimeZoneInformation.CurrentTimeZone;
TimeZoneInformation tziRemote = TimeZoneInformation.FromIndex(10);
// convert local time to UTC (could be abbreviated as DateTime.UtcNow)
DateTime dt = tziLocal.ToUniversalTime(DateTime.Now);
DateTime dt2 = tziRemote.FromUniversalTime(dt);

Points of interest

I have noticed that the time-zone names change when there are Windows updates, so you do not want to rely on them. I am keeping my fingers crossed that the Index attribute will not change.

Acknowledgements

I found the basis for my code on the internet but cannot remember where (it has been about two years ago). If you are the one whose code snippets I borrowed, drop me an email and I'll name you here!

History

None yet.

License

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

About the Author

kian01

Program Manager
The Boston Consulting Group
Germany Germany

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralIndex does not exist on Vista or Windows 2008 Pinmemberdwhearn3:53 4 Jul '08  
GeneralUsually ok, but can have some problems Pinmemberfuzzylintman16:07 6 Jun '08  
I've used this class for a long time now (the original TimeZoneInformation that you refer to). I've found that not only can the names change, but between different systems, even the indexes may not be consistent. That is to say, some time zones may exist on some computers but not on others. Thus if you have a program that tries to store the time zone by index, it is only reliable on that computer (and maybe not even then!) However in general, I've found this class to be relatively useful - just be careful with the potential problems.

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.

Permalink | Advertise | Privacy | Mobile
Web02 | 2.5.120529.1 | Last Updated 6 Jun 2008
Article Copyright 2008 by kian01
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid