.NET: Convert DateTime from One TimeZone to Another

Ever ran into the problem of figuring out the date and time in a time zone from a machine in another time zone in .NET?

Before discovering TimeZoneInfo.ConvertTime(), I used DateTime.Now.AddHours(), but before long, I found out that it doesn’t work properly for the intended purpose every time.

In my case, I overlooked that most of Arizona does not observe Daylight Saving Time (DST). That’s why simply using DateTime.Now.AddHours() does not work. If a machine is running in a time zone observing DST, then the calculated time for Arizona would be incorrect.

Daylight Saving Time (DST) Subdivisions

Image obtained from Wikipedia 

To calculate the date and time in Arizona -- or anywhere -- from any machine around the world, the following code is what you want. For other time zones, replace “US Mountain Standard Time” with the corresponding Time Zone ID value from the table below.

C# Code:

TimeZoneInfo timeZoneInfo; 
DateTime dateTime ; 
//Set the time zone information to US Mountain Standard Time 
timeZoneInfo = TimeZoneInfo.FindSystemTimeZoneById("US Mountain Standard Time"); 
//Get date and time in US Mountain Standard Time 
dateTime = TimeZoneInfo.ConvertTime(DateTime.Now, timeZoneInfo);
//Print out the date and time
Console.WriteLine(dateTime.ToString("yyyy-MM-dd HH-mm-ss")); 

I’m sure the above code would be easy to transcode to VB.NET, etc. Below is a table of the Time Zone IDs recognized by .NET

Time Zone ID

Time Zone Display Name

Morocco Standard Time

(GMT) Casablanca

GMT Standard Time

(GMT) Greenwich Mean Time : Dublin, Edinburgh, Lisbon, London

Greenwich Standard Time

(GMT) Monrovia, Reykjavik

W. Europe Standard Time

(GMT+01:00) Amsterdam, Berlin, Bern, Rome, Stockholm, Vienna

Central Europe Standard Time

(GMT+01:00) Belgrade, Bratislava, Budapest, Ljubljana, Prague

Romance Standard Time

(GMT+01:00) Brussels, Copenhagen, Madrid, Paris

Central European Standard Time

(GMT+01:00) Sarajevo, Skopje, Warsaw, Zagreb

W. Central Africa Standard Time

(GMT+01:00) West Central Africa

Jordan Standard Time

(GMT+02:00) Amman

GTB Standard Time

(GMT+02:00) Athens, Bucharest, Istanbul

Middle East Standard Time

(GMT+02:00) Beirut

Egypt Standard Time

(GMT+02:00) Cairo

South Africa Standard Time

(GMT+02:00) Harare, Pretoria

FLE Standard Time

(GMT+02:00) Helsinki, Kyiv, Riga, Sofia, Tallinn, Vilnius

Israel Standard Time

(GMT+02:00) Jerusalem

E. Europe Standard Time

(GMT+02:00) Minsk

Namibia Standard Time

(GMT+02:00) Windhoek

Arabic Standard Time

(GMT+03:00) Baghdad

Arab Standard Time

(GMT+03:00) Kuwait, Riyadh

Russian Standard Time

(GMT+03:00) Moscow, St. Petersburg, Volgograd

E. Africa Standard Time

(GMT+03:00) Nairobi

Georgian Standard Time

(GMT+03:00) Tbilisi

Iran Standard Time

(GMT+03:30) Tehran

Arabian Standard Time

(GMT+04:00) Abu Dhabi, Muscat

Azerbaijan Standard Time

(GMT+04:00) Baku

Mauritius Standard Time

(GMT+04:00) Port Louis

Caucasus Standard Time

(GMT+04:00) Yerevan

Afghanistan Standard Time

(GMT+04:30) Kabul

Ekaterinburg Standard Time

(GMT+05:00) Ekaterinburg

Pakistan Standard Time

(GMT+05:00) Islamabad, Karachi

West Asia Standard Time

(GMT+05:00) Tashkent

India Standard Time

(GMT+05:30) Chennai, Kolkata, Mumbai, New Delhi

Sri Lanka Standard Time

(GMT+05:30) Sri Jayawardenepura

Nepal Standard Time

(GMT+05:45) Kathmandu

N. Central Asia Standard Time

(GMT+06:00) Almaty, Novosibirsk

Central Asia Standard Time

(GMT+06:00) Astana, Dhaka

Myanmar Standard Time

(GMT+06:30) Yangon (Rangoon)

SE Asia Standard Time

(GMT+07:00) Bangkok, Hanoi, Jakarta

North Asia Standard Time

(GMT+07:00) Krasnoyarsk

China Standard Time

(GMT+08:00) Beijing, Chongqing, Hong Kong, Urumqi

North Asia East Standard Time

(GMT+08:00) Irkutsk, Ulaan Bataar

Singapore Standard Time

(GMT+08:00) Kuala Lumpur, Singapore

W. Australia Standard Time

(GMT+08:00) Perth

Taipei Standard Time

(GMT+08:00) Taipei

Tokyo Standard Time

(GMT+09:00) Osaka, Sapporo, Tokyo

Korea Standard Time

(GMT+09:00) Seoul

Yakutsk Standard Time

(GMT+09:00) Yakutsk

Cen. Australia Standard Time

(GMT+09:30) Adelaide

AUS Central Standard Time

(GMT+09:30) Darwin

E. Australia Standard Time

(GMT+10:00) Brisbane

AUS Eastern Standard Time

(GMT+10:00) Canberra, Melbourne, Sydney

West Pacific Standard Time

(GMT+10:00) Guam, Port Moresby

Tasmania Standard Time

(GMT+10:00) Hobart

Vladivostok Standard Time

(GMT+10:00) Vladivostok

Central Pacific Standard Time

(GMT+11:00) Magadan, Solomon Is., New Caledonia

New Zealand Standard Time

(GMT+12:00) Auckland, Wellington

Fiji Standard Time

(GMT+12:00) Fiji, Kamchatka, Marshall Is.

Tonga Standard Time

(GMT+13:00) Nuku'alofa

Azores Standard Time

(GMT-01:00) Azores

Cape Verde Standard Time

(GMT-01:00) Cape Verde Is.

Mid-Atlantic Standard Time

(GMT-02:00) Mid-Atlantic

E. South America Standard Time

(GMT-03:00) Brasilia

Argentina Standard Time

(GMT-03:00) Buenos Aires

SA Eastern Standard Time

(GMT-03:00) Georgetown

Greenland Standard Time

(GMT-03:00) Greenland

Montevideo Standard Time

(GMT-03:00) Montevideo

Newfoundland Standard Time

(GMT-03:30) Newfoundland

Atlantic Standard Time

(GMT-04:00) Atlantic Time (Canada)

SA Western Standard Time

(GMT-04:00) La Paz

Central Brazilian Standard Time

(GMT-04:00) Manaus

Pacific SA Standard Time

(GMT-04:00) Santiago

Venezuela Standard Time

(GMT-04:30) Caracas

SA Pacific Standard Time

(GMT-05:00) Bogota, Lima, Quito, Rio Branco

Eastern Standard Time

(GMT-05:00) Eastern Time (US & Canada)

US Eastern Standard Time

(GMT-05:00) Indiana (East)

Central America Standard Time

(GMT-06:00) Central America

Central Standard Time

(GMT-06:00) Central Time (US & Canada)

Central Standard Time (Mexico)

(GMT-06:00) Guadalajara, Mexico City, Monterrey

Canada Central Standard Time

(GMT-06:00) Saskatchewan

US Mountain Standard Time

(GMT-07:00) Arizona

Mountain Standard Time (Mexico)

(GMT-07:00) Chihuahua, La Paz, Mazatlan

Mountain Standard Time

(GMT-07:00) Mountain Time (US & Canada)

Pacific Standard Time

(GMT-08:00) Pacific Time (US & Canada)

Pacific Standard Time (Mexico)

(GMT-08:00) Tijuana, Baja California

Alaskan Standard Time

(GMT-09:00) Alaska

Hawaiian Standard Time

(GMT-10:00) Hawaii

Samoa Standard Time

(GMT-11:00) Midway Island, Samoa

Dateline Standard Time

(GMT-12:00) International Date Line West


Tags: .NET, ASP.NET, C#, Daylight Saving Time, DST, How To, Issues, Programming

Bookmark and Share

Comments