How to convert time to different time zone

Two days ago we had day light savings change. As we all call it "Spring Forward", the clocks were moved an hour ahead. That morning I noticed that one of the processes in our business process automation workflow triggered an hour early than it was scheduled for. I realized that the implementation had a bug and was not taking into account day light saving changes. After digging around, I found that .Net framework has TimeZoneInfo class that provides lot of useful methods that allow dealing with different time zones very easy. For example, in the implementation I needed to convert UTC time to New Zealand time. Previously I had time offset values saved in some configuration and then I was applying that offset. But that meant that I have to change that offset value when day light savings takes place. To make this implementation immune to these changes, you can use following methods depending on your need.

  • ConvertTimeFromUtc
  • ConvertTime
  • ConvertTimeBySystemTimeZoneId

You can look up documentation on MSDN to find more methods and their overloads. Following code snippet shows how I converted UTC time to New Zealand time.

var timeZoneInfo = TimeZoneInfo.FindSystemTimeZoneById("New Zealand Standard Time");
var nzTime = TimeZoneInfo.ConvertTimeFromUtc(DateTime.UtcNow, timeZoneInfo);

The critical part of this implementation is getting time zone information for a given time zone. This is done by calling FindSystemTimeZoneById method. It takes a string parameter that represents identity of that time zone. To get list of all available time zone ids, you can call TimeZoneInfo.GetSystemTimeZones method to get complete list available on your server. The following list has been generated on my machine using this method. The second column in this list are time zone Ids.

Dateline Daylight Time - Dateline Standard Time
UTC-11 - UTC-11
Hawaiian Daylight Time - Hawaiian Standard Time
Alaskan Daylight Time - Alaskan Standard Time
Pacific Daylight Time (Mexico) - Pacific Standard Time (Mexico)
Pacific Daylight Time - Pacific Standard Time
US Mountain Daylight Time - US Mountain Standard Time
Mountain Daylight Time (Mexico) - Mountain Standard Time (Mexico)
Mountain Daylight Time - Mountain Standard Time
Central America Daylight Time - Central America Standard Time
Central Daylight Time - Central Standard Time
Central Daylight Time (Mexico) - Central Standard Time (Mexico)
Canada Central Daylight Time - Canada Central Standard Time
SA Pacific Daylight Time - SA Pacific Standard Time
Eastern Daylight Time - Eastern Standard Time
US Eastern Daylight Time - US Eastern Standard Time
Venezuela Daylight Time - Venezuela Standard Time
Paraguay Daylight Time - Paraguay Standard Time
Atlantic Daylight Time - Atlantic Standard Time
Central Brazilian Daylight Time - Central Brazilian Standard Time
SA Western Daylight Time - SA Western Standard Time
Pacific SA Daylight Time - Pacific SA Standard Time
Newfoundland Daylight Time - Newfoundland Standard Time
E. South America Daylight Time - E. South America Standard Time
Argentina Daylight Time - Argentina Standard Time
SA Eastern Daylight Time - SA Eastern Standard Time
Greenland Daylight Time - Greenland Standard Time
Montevideo Daylight Time - Montevideo Standard Time
Bahia Daylight Time - Bahia Standard Time
UTC-02 - UTC-02
Mid-Atlantic Daylight Time - Mid-Atlantic Standard Time
Azores Daylight Time - Azores Standard Time
Cape Verde Daylight Time - Cape Verde Standard Time
Morocco Daylight Time - Morocco Standard Time
Coordinated Universal Time - UTC
GMT Daylight Time - GMT Standard Time
Greenwich Daylight Time - Greenwich Standard Time
W. Europe Daylight Time - W. Europe Standard Time
Central Europe Daylight Time - Central Europe Standard Time
Romance Daylight Time - Romance Standard Time
Central European Daylight Time - Central European Standard Time
W. Central Africa Daylight Time - W. Central Africa Standard Time
Namibia Daylight Time - Namibia Standard Time
GTB Daylight Time - GTB Standard Time
Middle East Daylight Time - Middle East Standard Time
Egypt Daylight Time - Egypt Standard Time
Syria Daylight Time - Syria Standard Time
E. Europe Daylight Time - E. Europe Standard Time
South Africa Daylight Time - South Africa Standard Time
FLE Daylight Time - FLE Standard Time
Turkey Daylight Time - Turkey Standard Time
Jerusalem Daylight Time - Israel Standard Time
Libya Daylight Time - Libya Standard Time
Jordan Daylight Time - Jordan Standard Time
Arabic Daylight Time - Arabic Standard Time
Kaliningrad Daylight Time - Kaliningrad Standard Time
Arab Daylight Time - Arab Standard Time
E. Africa Daylight Time - E. Africa Standard Time
Iran Daylight Time - Iran Standard Time
Arabian Daylight Time - Arabian Standard Time
Azerbaijan Daylight Time - Azerbaijan Standard Time
Russian Daylight Time - Russian Standard Time
Mauritius Daylight Time - Mauritius Standard Time
Georgian Daylight Time - Georgian Standard Time
Caucasus Daylight Time - Caucasus Standard Time
Afghanistan Daylight Time - Afghanistan Standard Time
West Asia Daylight Time - West Asia Standard Time
Pakistan Daylight Time - Pakistan Standard Time
India Daylight Time - India Standard Time
Sri Lanka Daylight Time - Sri Lanka Standard Time
Nepal Daylight Time - Nepal Standard Time
Central Asia Daylight Time - Central Asia Standard Time
Bangladesh Daylight Time - Bangladesh Standard Time
Ekaterinburg Daylight Time - Ekaterinburg Standard Time
Myanmar Daylight Time - Myanmar Standard Time
SE Asia Daylight Time - SE Asia Standard Time
N. Central Asia Daylight Time - N. Central Asia Standard Time
China Daylight Time - China Standard Time
North Asia Daylight Time - North Asia Standard Time
Malay Peninsula Daylight Time - Singapore Standard Time
W. Australia Daylight Time - W. Australia Standard Time
Taipei Daylight Time - Taipei Standard Time
Ulaanbaatar Daylight Time - Ulaanbaatar Standard Time
North Asia East Daylight Time - North Asia East Standard Time
Tokyo Daylight Time - Tokyo Standard Time
Korea Daylight Time - Korea Standard Time
Cen. Australia Daylight Time - Cen. Australia Standard Time
AUS Central Daylight Time - AUS Central Standard Time
E. Australia Daylight Time - E. Australia Standard Time
AUS Eastern Daylight Time - AUS Eastern Standard Time
West Pacific Daylight Time - West Pacific Standard Time
Tasmania Daylight Time - Tasmania Standard Time
Yakutsk Daylight Time - Yakutsk Standard Time
Central Pacific Daylight Time - Central Pacific Standard Time
Vladivostok Daylight Time - Vladivostok Standard Time
New Zealand Daylight Time - New Zealand Standard Time
UTC+12 - UTC+12
Fiji Daylight Time - Fiji Standard Time
Magadan Daylight Time - Magadan Standard Time
Kamchatka Daylight Time - Kamchatka Standard Time
Tonga Daylight Time - Tonga Standard Time
Samoa Daylight Time - Samoa Standard Time
comments powered by Disqus

Search

Social

Weather

13.8 °C / 56.9 °F

weather conditions Clouds

Monthly Posts

Blog Tags