You are on page 1of 3

.

NET: Convert DateTime from One TimeZone to Another


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: TimeZoneInfotimeZoneInfo; DateTimedateTime; //SetthetimezoneinformationtoUSMountainStandardTime timeZoneInfo=TimeZoneInfo.FindSystemTimeZoneById("USMountainStandardTime"); //GetdateandtimeinUSMountainStandardTime dateTime=TimeZoneInfo.ConvertTime(DateTime.Now,timeZoneInfo); //Printoutthedateandtime Console.WriteLine(dateTime.ToString("yyyyMMddHHmmss")); VB.NetCode
Dim timeZoneInfo__1 As TimeZoneInfo Dim dateTime__2 As DateTime 'Set the time zone information to US Mountain Standard Time timeZoneInfo__1 = TimeZoneInfo.FindSystemTimeZoneById("US Mountain Standard Time") 'Get date and time in US Mountain Standard Time dateTime__2 = TimeZoneInfo.ConvertTime(DateTime.Now, timeZoneInfo__1) 'Print out the date and time Console.WriteLine(dateTime__2.ToString("yyyy-MM-dd HH-mm-ss"))


Time Zone ID Morocco Standard Time GMT Standard Time Greenwich Standard Time W. Europe Standard Time Central Europe Standard Time Romance Standard Time Central European Standard Time W. Central Africa Standard Time Jordan Standard Time GTB Standard Time Middle East Standard Time Egypt Standard Time South Africa Standard Time FLE Standard Time Israel Standard Time E. Europe Standard Time Namibia Standard Time Arabic Standard Time Arab Standard Time Russian Standard Time E. Africa Standard Time Georgian Standard Time Iran Standard Time Arabian Standard Time Time Zone Display Name (GMT) Casablanca (GMT) Greenwich Mean Time : Dublin, Edinburgh, Lisbon, London (GMT) Monrovia, Reykjavik (GMT+01:00) Amsterdam, Berlin, Bern, Rome, Stockholm, Vienna (GMT+01:00) Belgrade, Bratislava, Budapest, Ljubljana, Prague (GMT+01:00) Brussels, Copenhagen, Madrid, Paris (GMT+01:00) Sarajevo, Skopje, Warsaw, Zagreb (GMT+01:00) West Central Africa (GMT+02:00) Amman (GMT+02:00) Athens, Bucharest, Istanbul (GMT+02:00) Beirut (GMT+02:00) Cairo (GMT+02:00) Harare, Pretoria (GMT+02:00) Helsinki, Kyiv, Riga, Sofia, Tallinn, Vilnius (GMT+02:00) Jerusalem (GMT+02:00) Minsk (GMT+02:00) Windhoek (GMT+03:00) Baghdad (GMT+03:00) Kuwait, Riyadh (GMT+03:00) Moscow, St. Petersburg, Volgograd (GMT+03:00) Nairobi (GMT+03:00) Tbilisi (GMT+03:30) Tehran (GMT+04:00) Abu Dhabi, Muscat

Azerbaijan Standard Time Mauritius Standard Time Caucasus Standard Time Afghanistan Standard Time Ekaterinburg Standard Time Pakistan Standard Time West Asia Standard Time India Standard Time Sri Lanka Standard Time Nepal Standard Time N. Central Asia Standard Time Central Asia Standard Time Myanmar Standard Time SE Asia Standard Time North Asia Standard Time China Standard Time North Asia East Standard Time Singapore Standard Time W. Australia Standard Time Taipei Standard Time Tokyo Standard Time Korea Standard Time Yakutsk Standard Time Cen. Australia Standard Time AUS Central Standard Time E. Australia Standard Time AUS Eastern Standard Time West Pacific Standard Time Tasmania Standard Time Vladivostok Standard Time Central Pacific Standard Time New Zealand Standard Time Fiji Standard Time Tonga Standard Time Azores Standard Time Cape Verde Standard Time Mid-Atlantic Standard Time E. South America Standard Time Argentina Standard Time SA Eastern Standard Time Greenland Standard Time Montevideo Standard Time Newfoundland Standard Time Atlantic Standard Time SA Western Standard Time Central Brazilian Standard Time Pacific SA Standard Time Venezuela Standard Time SA Pacific Standard Time Eastern Standard Time US Eastern Standard Time

(GMT+04:00) Baku (GMT+04:00) Port Louis (GMT+04:00) Yerevan (GMT+04:30) Kabul (GMT+05:00) Ekaterinburg (GMT+05:00) Islamabad, Karachi (GMT+05:00) Tashkent (GMT+05:30) Chennai, Kolkata, Mumbai, New Delhi (GMT+05:30) Sri Jayawardenepura (GMT+05:45) Kathmandu (GMT+06:00) Almaty, Novosibirsk (GMT+06:00) Astana, Dhaka (GMT+06:30) Yangon (Rangoon) (GMT+07:00) Bangkok, Hanoi, Jakarta (GMT+07:00) Krasnoyarsk (GMT+08:00) Beijing, Chongqing, Hong Kong, Urumqi (GMT+08:00) Irkutsk, Ulaan Bataar (GMT+08:00) Kuala Lumpur, Singapore (GMT+08:00) Perth (GMT+08:00) Taipei (GMT+09:00) Osaka, Sapporo, Tokyo (GMT+09:00) Seoul (GMT+09:00) Yakutsk (GMT+09:30) Adelaide (GMT+09:30) Darwin (GMT+10:00) Brisbane (GMT+10:00) Canberra, Melbourne, Sydney (GMT+10:00) Guam, Port Moresby (GMT+10:00) Hobart (GMT+10:00) Vladivostok (GMT+11:00) Magadan, Solomon Is., New Caledonia (GMT+12:00) Auckland, Wellington (GMT+12:00) Fiji, Kamchatka, Marshall Is. (GMT+13:00) Nuku'alofa (GMT-01:00) Azores (GMT-01:00) Cape Verde Is. (GMT-02:00) Mid-Atlantic (GMT-03:00) Brasilia (GMT-03:00) Buenos Aires (GMT-03:00) Georgetown (GMT-03:00) Greenland (GMT-03:00) Montevideo (GMT-03:30) Newfoundland (GMT-04:00) Atlantic Time (Canada) (GMT-04:00) La Paz (GMT-04:00) Manaus (GMT-04:00) Santiago (GMT-04:30) Caracas (GMT-05:00) Bogota, Lima, Quito, Rio Branco (GMT-05:00) Eastern Time (US & Canada) (GMT-05:00) Indiana (East)

Central America Standard Time Central Standard Time Central Standard Time (Mexico) Canada Central Standard Time US Mountain Standard Time Mountain Standard Time (Mexico) Mountain Standard Time Pacific Standard Time Pacific Standard Time (Mexico) Alaskan Standard Time Hawaiian Standard Time Samoa Standard Time Dateline Standard Time

(GMT-06:00) Central America (GMT-06:00) Central Time (US & Canada) (GMT-06:00) Guadalajara, Mexico City, Monterrey (GMT-06:00) Saskatchewan (GMT-07:00) Arizona (GMT-07:00) Chihuahua, La Paz, Mazatlan (GMT-07:00) Mountain Time (US & Canada) (GMT-08:00) Pacific Time (US & Canada) (GMT-08:00) Tijuana, Baja California (GMT-09:00) Alaska (GMT-10:00) Hawaii (GMT-11:00) Midway Island, Samoa (GMT-12:00) International Date Line West

Beautiful! I did, however, find an easier way. You could also simply write: Console.WriteLine (TimeZoneInfo.ConvertTimeBySystemTimeZoneId (now, "US Mountain Standard Time").ToString ("yyyy-MM-dd HHmm-ss")); which does it all in one hit. ConvertTimeBySystemTimeZoneId takes a time and a zone and returns a datetime. Pretty spiffy, huh?

You might also like