Retrieve date and local time of an arbitrary day in an arbitrary Timezone, considering daylight saving time

Asked

Viewed 1,357 times

12

I want to show the day, hour, minute etc. in an arbitrary Timezone, in an arbitrary day. How to talk about timezone conversion is always difficult and confusing, and how to talk about timezone conversions javascript is even more difficult and confusing, I will try kind of formalize the issue:

I seek a function f(t, s) that:

  • given the timestamp 1357041600 (which 2013/1/1 12:00:00 UTC) and the string "America/São Paulo", satisfy the equality below:

    f(1357041600, "America/São Paulo") == "2013/01/01 10:00:00"
    
  • given the timestamp 1372680000 (2013/07/01 12:00:00 UTC), meet equality below:

    f(1357041600, "America/São Paulo") == "2013/07/01 09:00:00"
    
  • always behave this way regardless of the Timezone of the machine where the browser runs (even if it is, say "Europe/London" or "America/Los Angeles").

  • always behave this way regardless of the time on the machine where the browser runs (even if it is 2014/02/05 19:32, or 2002/08/04 07:12)

  • and, finally, do not communicate with code running on any server (because I myself am almost appealing to this :) )

That’s possible?

  • Did you take a look at my answer? I’m curious if it solves your problem.

3 answers

7

This function does not exist native in javascript, it has to be done "by hand" or by using a library.

So here’s one I made now (between last night and today):

First, state all time zones on a JSON:

var olson = '{"result":[{"TimeZoneId":"Africa/Abidjan","DST":"0","GMT":"0"},{"TimeZoneId":"Africa/Accra","DST":"0","GMT":"0"},{"TimeZoneId":"Africa/Addis Ababa","DST":"3","GMT":"3"},{"TimeZoneId":"Africa/Algiers","DST":"1","GMT":"1"},{"TimeZoneId":"Africa/Asmara","DST":"3","GMT":"3"},{"TimeZoneId":"Africa/Bamako","DST":"0","GMT":"0"},{"TimeZoneId":"Africa/Bangui","DST":"1","GMT":"1"},{"TimeZoneId":"Africa/Banjul","DST":"0","GMT":"0"},{"TimeZoneId":"Africa/Bissau","DST":"0","GMT":"0"},{"TimeZoneId":"Africa/Blantyre","DST":"2","GMT":"2"},{"TimeZoneId":"Africa/Brazzaville","DST":"1","GMT":"1"},{"TimeZoneId":"Africa/Bujumbura","DST":"2","GMT":"2"},{"TimeZoneId":"Africa/Cairo","DST":"3","GMT":"2"},{"TimeZoneId":"Africa/Casablanca","DST":"0","GMT":"0"},{"TimeZoneId":"Africa/Ceuta","DST":"2","GMT":"1"},{"TimeZoneId":"Africa/Conakry","DST":"0","GMT":"0"},{"TimeZoneId":"Africa/Dakar","DST":"0","GMT":"0"},{"TimeZoneId":"Africa/Dar es Salaam","DST":"3","GMT":"3"},{"TimeZoneId":"Africa/Djibouti","DST":"3","GMT":"3"},{"TimeZoneId":"Africa/Douala","DST":"1","GMT":"1"},{"TimeZoneId":"Africa/El Aaiun","DST":"0","GMT":"0"},{"TimeZoneId":"Africa/Freetown","DST":"0","GMT":"0"},{"TimeZoneId":"Africa/Gaborone","DST":"2","GMT":"2"},{"TimeZoneId":"Africa/Harare","DST":"2","GMT":"2"},{"TimeZoneId":"Africa/Johannesburg","DST":"2","GMT":"2"},{"TimeZoneId":"Africa/Kampala","DST":"3","GMT":"3"},{"TimeZoneId":"Africa/Khartoum","DST":"3","GMT":"3"},{"TimeZoneId":"Africa/Kigali","DST":"2","GMT":"2"},{"TimeZoneId":"Africa/Kinshasa","DST":"1","GMT":"1"},{"TimeZoneId":"Africa/Lagos","DST":"1","GMT":"1"},{"TimeZoneId":"Africa/Libreville","DST":"1","GMT":"1"},{"TimeZoneId":"Africa/Lome","DST":"0","GMT":"0"},{"TimeZoneId":"Africa/Luanda","DST":"1","GMT":"1"},{"TimeZoneId":"Africa/Lubumbashi","DST":"2","GMT":"2"},{"TimeZoneId":"Africa/Lusaka","DST":"2","GMT":"2"},{"TimeZoneId":"Africa/Malabo","DST":"1","GMT":"1"},{"TimeZoneId":"Africa/Maputo","DST":"2","GMT":"2"},{"TimeZoneId":"Africa/Maseru","DST":"2","GMT":"2"},{"TimeZoneId":"Africa/Mbabane","DST":"2","GMT":"2"},{"TimeZoneId":"Africa/Mogadishu","DST":"3","GMT":"3"},{"TimeZoneId":"Africa/Monrovia","DST":"0","GMT":"0"},{"TimeZoneId":"Africa/Nairobi","DST":"3","GMT":"3"},{"TimeZoneId":"Africa/Ndjamena","DST":"1","GMT":"1"},{"TimeZoneId":"Africa/Niamey","DST":"1","GMT":"1"},{"TimeZoneId":"Africa/Nouakchott","DST":"0","GMT":"0"},{"TimeZoneId":"Africa/Ouagadougou","DST":"0","GMT":"0"},{"TimeZoneId":"Africa/Porto/Novo","DST":"1","GMT":"1"},{"TimeZoneId":"Africa/Sao Tome","DST":"0","GMT":"0"},{"TimeZoneId":"Africa/Tripoli","DST":"2","GMT":"2"},{"TimeZoneId":"Africa/Tunis","DST":"2","GMT":"1"},{"TimeZoneId":"Africa/Windhoek","DST":"1","GMT":"2"},{"TimeZoneId":"America/Adak","DST":"-9","GMT":"-10"},{"TimeZoneId":"America/Anchorage","DST":"-8","GMT":"-9"},{"TimeZoneId":"America/Anguilla","DST":"-4","GMT":"-4"},{"TimeZoneId":"America/Antigua","DST":"-4","GMT":"-4"},{"TimeZoneId":"America/Araguaina","DST":"-3","GMT":"-3"},{"TimeZoneId":"America/Argentina/Buenos Aires","DST":"-3","GMT":"-3"},{"TimeZoneId":"America/Argentina/Catamarca","DST":"-3","GMT":"-3"},{"TimeZoneId":"America/Argentina/Cordoba","DST":"-3","GMT":"-3"},{"TimeZoneId":"America/Argentina/Jujuy","DST":"-3","GMT":"-3"},{"TimeZoneId":"America/Argentina/La Rioja","DST":"-3","GMT":"-3"},{"TimeZoneId":"America/Argentina/Mendoza","DST":"-3","GMT":"-3"},{"TimeZoneId":"America/Argentina/Rio Gallegos","DST":"-3","GMT":"-3"},{"TimeZoneId":"America/Argentina/Salta","DST":"-3","GMT":"-3"},{"TimeZoneId":"America/Argentina/San Juan","DST":"-3","GMT":"-3"},{"TimeZoneId":"America/Argentina/San Luis","DST":"-4","GMT":"-3"},{"TimeZoneId":"America/Argentina/Tucuman","DST":"-3","GMT":"-3"},{"TimeZoneId":"America/Argentina/Ushuaia","DST":"-3","GMT":"-3"},{"TimeZoneId":"America/Aruba","DST":"-4","GMT":"-4"},{"TimeZoneId":"America/Asuncion","DST":"-4","GMT":"-3"},{"TimeZoneId":"America/Atikokan","DST":"-5","GMT":"-5"},{"TimeZoneId":"America/Bahia","DST":"-3","GMT":"-3"},{"TimeZoneId":"America/Barbados","DST":"-4","GMT":"-4"},{"TimeZoneId":"America/Belem","DST":"-3","GMT":"-3"},{"TimeZoneId":"America/Belize","DST":"-6","GMT":"-6"},{"TimeZoneId":"America/Blanc/Sablon","DST":"-4","GMT":"-4"},{"TimeZoneId":"America/Boa Vista","DST":"-4","GMT":"-4"},{"TimeZoneId":"America/Bogota","DST":"-5","GMT":"-5"},{"TimeZoneId":"America/Boise","DST":"-6","GMT":"-7"},{"TimeZoneId":"America/Cambridge Bay","DST":"-6","GMT":"-7"},{"TimeZoneId":"America/Campo Grande","DST":"-4","GMT":"-3"},{"TimeZoneId":"America/Cancun","DST":"-5","GMT":"-6"},{"TimeZoneId":"America/Caracas","DST":"-4.5","GMT":"-4.5"},{"TimeZoneId":"America/Cayenne","DST":"-3","GMT":"-3"},{"TimeZoneId":"America/Cayman","DST":"-5","GMT":"-5"},{"TimeZoneId":"America/Chicago","DST":"-5","GMT":"-6"},{"TimeZoneId":"America/Chihuahua","DST":"-6","GMT":"-7"},{"TimeZoneId":"America/Costa Rica","DST":"-6","GMT":"-6"},{"TimeZoneId":"America/Cuiaba","DST":"-4","GMT":"-3"},{"TimeZoneId":"America/Curacao","DST":"-4","GMT":"-4"},{"TimeZoneId":"America/Danmarkshavn","DST":"0","GMT":"0"},{"TimeZoneId":"America/Dawson","DST":"-7","GMT":"-8"},{"TimeZoneId":"America/Dawson Creek","DST":"-7","GMT":"-7"},{"TimeZoneId":"America/Denver","DST":"-6","GMT":"-7"},{"TimeZoneId":"America/Detroit","DST":"-4","GMT":"-5"},{"TimeZoneId":"America/Dominica","DST":"-4","GMT":"-4"},{"TimeZoneId":"America/Edmonton","DST":"-6","GMT":"-7"},{"TimeZoneId":"America/Eirunepe","DST":"-4","GMT":"-4"},{"TimeZoneId":"America/El Salvador","DST":"-6","GMT":"-6"},{"TimeZoneId":"America/Fortaleza","DST":"-3","GMT":"-3"},{"TimeZoneId":"America/Glace Bay","DST":"-3","GMT":"-4"},{"TimeZoneId":"America/Godthab","DST":"-2","GMT":"-3"},{"TimeZoneId":"America/Goose Bay","DST":"-3","GMT":"-4"},{"TimeZoneId":"America/Grand Turk","DST":"-4","GMT":"-5"},{"TimeZoneId":"America/Grenada","DST":"-4","GMT":"-4"},{"TimeZoneId":"America/Guadeloupe","DST":"-4","GMT":"-4"},{"TimeZoneId":"America/Guatemala","DST":"-6","GMT":"-6"},{"TimeZoneId":"America/Guayaquil","DST":"-5","GMT":"-5"},{"TimeZoneId":"America/Guyana","DST":"-4","GMT":"-4"},{"TimeZoneId":"America/Halifax","DST":"-3","GMT":"-4"},{"TimeZoneId":"America/Havana","DST":"-4","GMT":"-5"},{"TimeZoneId":"America/Hermosillo","DST":"-7","GMT":"-7"},{"TimeZoneId":"America/Indiana/Indianapolis","DST":"-4","GMT":"-5"},{"TimeZoneId":"America/Indiana/Knox","DST":"-5","GMT":"-6"},{"TimeZoneId":"America/Indiana/Marengo","DST":"-4","GMT":"-5"},{"TimeZoneId":"America/Indiana/Petersburg","DST":"-4","GMT":"-5"},{"TimeZoneId":"America/Indiana/Tell City","DST":"-5","GMT":"-6"},{"TimeZoneId":"America/Indiana/Vevay","DST":"-4","GMT":"-5"},{"TimeZoneId":"America/Indiana/Vincennes","DST":"-4","GMT":"-5"},{"TimeZoneId":"America/Indiana/Winamac","DST":"-4","GMT":"-5"},{"TimeZoneId":"America/Inuvik","DST":"-6","GMT":"-7"},{"TimeZoneId":"America/Iqaluit","DST":"-4","GMT":"-5"},{"TimeZoneId":"America/Jamaica","DST":"-5","GMT":"-5"},{"TimeZoneId":"America/Juneau","DST":"-8","GMT":"-9"},{"TimeZoneId":"America/Kentucky/Louisville","DST":"-4","GMT":"-5"},{"TimeZoneId":"America/Kentucky/Monticello","DST":"-4","GMT":"-5"},{"TimeZoneId":"America/La Paz","DST":"-4","GMT":"-4"},{"TimeZoneId":"America/Lima","DST":"-5","GMT":"-5"},{"TimeZoneId":"America/Los Angeles","DST":"-7","GMT":"-8"},{"TimeZoneId":"America/Maceio","DST":"-3","GMT":"-3"},{"TimeZoneId":"America/Managua","DST":"-6","GMT":"-6"},{"TimeZoneId":"America/Manaus","DST":"-4","GMT":"-4"},{"TimeZoneId":"America/Marigot","DST":"-4","GMT":"-4"},{"TimeZoneId":"America/Martinique","DST":"-4","GMT":"-4"},{"TimeZoneId":"America/Matamoros","DST":"0","GMT":"0"},{"TimeZoneId":"America/Mazatlan","DST":"-6","GMT":"-7"},{"TimeZoneId":"America/Menominee","DST":"-5","GMT":"-6"},{"TimeZoneId":"America/Merida","DST":"-5","GMT":"-6"},{"TimeZoneId":"America/Mexico City","DST":"-5","GMT":"-6"},{"TimeZoneId":"America/Miquelon","DST":"-2","GMT":"-3"},{"TimeZoneId":"America/Moncton","DST":"-3","GMT":"-4"},{"TimeZoneId":"America/Monterrey","DST":"-5","GMT":"-6"},{"TimeZoneId":"America/Montevideo","DST":"-3","GMT":"-2"},{"TimeZoneId":"America/Montreal","DST":"-4","GMT":"-5"},{"TimeZoneId":"America/Montserrat","DST":"-4","GMT":"-4"},{"TimeZoneId":"America/Nassau","DST":"-4","GMT":"-5"},{"TimeZoneId":"America/New York","DST":"-4","GMT":"-5"},{"TimeZoneId":"America/Nipigon","DST":"-4","GMT":"-5"},{"TimeZoneId":"America/Nome","DST":"-8","GMT":"-9"},{"TimeZoneId":"America/Noronha","DST":"-2","GMT":"-2"},{"TimeZoneId":"America/North Dakota/Center","DST":"-5","GMT":"-6"},{"TimeZoneId":"America/North Dakota/New Salem","DST":"-5","GMT":"-6"},{"TimeZoneId":"America/Ojinaga","DST":"0","GMT":"0"},{"TimeZoneId":"America/Panama","DST":"-5","GMT":"-5"},{"TimeZoneId":"America/Pangnirtung","DST":"-4","GMT":"-5"},{"TimeZoneId":"America/Paramaribo","DST":"-3","GMT":"-3"},{"TimeZoneId":"America/Phoenix","DST":"-7","GMT":"-7"},{"TimeZoneId":"America/Port/au/Prince","DST":"-5","GMT":"-5"},{"TimeZoneId":"America/Port of Spain","DST":"-4","GMT":"-4"},{"TimeZoneId":"America/Porto Velho","DST":"-4","GMT":"-4"},{"TimeZoneId":"America/Puerto Rico","DST":"-4","GMT":"-4"},{"TimeZoneId":"America/Rainy River","DST":"-5","GMT":"-6"},{"TimeZoneId":"America/Rankin Inlet","DST":"-5","GMT":"-6"},{"TimeZoneId":"America/Recife","DST":"-3","GMT":"-3"},{"TimeZoneId":"America/Regina","DST":"-6","GMT":"-6"},{"TimeZoneId":"America/Resolute","DST":"-5","GMT":"-5"},{"TimeZoneId":"America/Rio Branco","DST":"-4","GMT":"-4"},{"TimeZoneId":"America/Santa Isabel","DST":"0","GMT":"0"},{"TimeZoneId":"America/Santarem","DST":"-3","GMT":"-3"},{"TimeZoneId":"America/Santiago","DST":"-4","GMT":"-3"},{"TimeZoneId":"America/Santo Domingo","DST":"-4","GMT":"-4"},{"TimeZoneId":"America/Sao Paulo","DST":"-3","GMT":"-2"},{"TimeZoneId":"America/Scoresbysund","DST":"0","GMT":"-1"},{"TimeZoneId":"America/Shiprock","DST":"-6","GMT":"-7"},{"TimeZoneId":"America/St Barthelemy","DST":"-4","GMT":"-4"},{"TimeZoneId":"America/St Johns","DST":"-2.5","GMT":"-3.5"},{"TimeZoneId":"America/St Kitts","DST":"-4","GMT":"-4"},{"TimeZoneId":"America/St Lucia","DST":"-4","GMT":"-4"},{"TimeZoneId":"America/St Thomas","DST":"-4","GMT":"-4"},{"TimeZoneId":"America/St Vincent","DST":"-4","GMT":"-4"},{"TimeZoneId":"America/Swift Current","DST":"-6","GMT":"-6"},{"TimeZoneId":"America/Tegucigalpa","DST":"-6","GMT":"-6"},{"TimeZoneId":"America/Thule","DST":"-3","GMT":"-4"},{"TimeZoneId":"America/Thunder Bay","DST":"-4","GMT":"-5"},{"TimeZoneId":"America/Tijuana","DST":"-7","GMT":"-8"},{"TimeZoneId":"America/Toronto","DST":"-4","GMT":"-5"},{"TimeZoneId":"America/Tortola","DST":"-4","GMT":"-4"},{"TimeZoneId":"America/Vancouver","DST":"-7","GMT":"-8"},{"TimeZoneId":"America/Whitehorse","DST":"-7","GMT":"-8"},{"TimeZoneId":"America/Winnipeg","DST":"-5","GMT":"-6"},{"TimeZoneId":"America/Yakutat","DST":"-8","GMT":"-9"},{"TimeZoneId":"America/Yellowknife","DST":"-6","GMT":"-7"},{"TimeZoneId":"Antarctica/Casey","DST":"11","GMT":"11"},{"TimeZoneId":"Antarctica/Davis","DST":"5","GMT":"5"},{"TimeZoneId":"Antarctica/DumontDUrville","DST":"10","GMT":"10"},{"TimeZoneId":"Antarctica/Mawson","DST":"5","GMT":"5"},{"TimeZoneId":"Antarctica/McMurdo","DST":"12","GMT":"13"},{"TimeZoneId":"Antarctica/Palmer","DST":"-4","GMT":"-3"},{"TimeZoneId":"Antarctica/Rothera","DST":"-3","GMT":"-3"},{"TimeZoneId":"Antarctica/South Pole","DST":"12","GMT":"13"},{"TimeZoneId":"Antarctica/Syowa","DST":"3","GMT":"3"},{"TimeZoneId":"Antarctica/Vostok","DST":"6","GMT":"6"},{"TimeZoneId":"Arctic/Longyearbyen","DST":"2","GMT":"1"},{"TimeZoneId":"Asia/Aden","DST":"3","GMT":"3"},{"TimeZoneId":"Asia/Almaty","DST":"6","GMT":"6"},{"TimeZoneId":"Asia/Amman","DST":"3","GMT":"2"},{"TimeZoneId":"Asia/Anadyr","DST":"13","GMT":"12"},{"TimeZoneId":"Asia/Aqtau","DST":"5","GMT":"5"},{"TimeZoneId":"Asia/Aqtobe","DST":"5","GMT":"5"},{"TimeZoneId":"Asia/Ashgabat","DST":"5","GMT":"5"},{"TimeZoneId":"Asia/Baghdad","DST":"3","GMT":"3"},{"TimeZoneId":"Asia/Bahrain","DST":"3","GMT":"3"},{"TimeZoneId":"Asia/Baku","DST":"5","GMT":"4"},{"TimeZoneId":"Asia/Bangkok","DST":"7","GMT":"7"},{"TimeZoneId":"Asia/Beirut","DST":"3","GMT":"2"},{"TimeZoneId":"Asia/Bishkek","DST":"6","GMT":"6"},{"TimeZoneId":"Asia/Brunei","DST":"8","GMT":"8"},{"TimeZoneId":"Asia/Choibalsan","DST":"8","GMT":"8"},{"TimeZoneId":"Asia/Chongqing","DST":"8","GMT":"8"},{"TimeZoneId":"Asia/Colombo","DST":"5.5","GMT":"5.5"},{"TimeZoneId":"Asia/Damascus","DST":"3","GMT":"2"},{"TimeZoneId":"Asia/Dhaka","DST":"7","GMT":"6"},{"TimeZoneId":"Asia/Dili","DST":"9","GMT":"9"},{"TimeZoneId":"Asia/Dubai","DST":"4","GMT":"4"},{"TimeZoneId":"Asia/Dushanbe","DST":"5","GMT":"5"},{"TimeZoneId":"Asia/Gaza","DST":"3","GMT":"2"},{"TimeZoneId":"Asia/Harbin","DST":"8","GMT":"8"},{"TimeZoneId":"Asia/Ho Chi Minh","DST":"7","GMT":"7"},{"TimeZoneId":"Asia/Hong Kong","DST":"8","GMT":"8"},{"TimeZoneId":"Asia/Hovd","DST":"7","GMT":"7"},{"TimeZoneId":"Asia/Irkutsk","DST":"9","GMT":"8"},{"TimeZoneId":"Asia/Jakarta","DST":"7","GMT":"7"},{"TimeZoneId":"Asia/Jayapura","DST":"9","GMT":"9"},{"TimeZoneId":"Asia/Jerusalem","DST":"3","GMT":"2"},{"TimeZoneId":"Asia/Kabul","DST":"4.5","GMT":"4.5"},{"TimeZoneId":"Asia/Kamchatka","DST":"13","GMT":"12"},{"TimeZoneId":"Asia/Karachi","DST":"6","GMT":"5"},{"TimeZoneId":"Asia/Kashgar","DST":"8","GMT":"8"},{"TimeZoneId":"Asia/Kathmandu","DST":"5.75","GMT":"5.75"},{"TimeZoneId":"Asia/Kolkata","DST":"5.5","GMT":"5.5"},{"TimeZoneId":"Asia/Krasnoyarsk","DST":"8","GMT":"7"},{"TimeZoneId":"Asia/Kuala Lumpur","DST":"8","GMT":"8"},{"TimeZoneId":"Asia/Kuching","DST":"8","GMT":"8"},{"TimeZoneId":"Asia/Kuwait","DST":"3","GMT":"3"},{"TimeZoneId":"Asia/Macau","DST":"8","GMT":"8"},{"TimeZoneId":"Asia/Magadan","DST":"12","GMT":"11"},{"TimeZoneId":"Asia/Makassar","DST":"8","GMT":"8"},{"TimeZoneId":"Asia/Manila","DST":"8","GMT":"8"},{"TimeZoneId":"Asia/Muscat","DST":"4","GMT":"4"},{"TimeZoneId":"Asia/Nicosia","DST":"3","GMT":"2"},{"TimeZoneId":"Asia/Novokuznetsk","DST":"7","GMT":"7"},{"TimeZoneId":"Asia/Novosibirsk","DST":"7","GMT":"6"},{"TimeZoneId":"Asia/Omsk","DST":"7","GMT":"6"},{"TimeZoneId":"Asia/Oral","DST":"5","GMT":"5"},{"TimeZoneId":"Asia/Phnom Penh","DST":"7","GMT":"7"},{"TimeZoneId":"Asia/Pontianak","DST":"7","GMT":"7"},{"TimeZoneId":"Asia/Pyongyang","DST":"9","GMT":"9"},{"TimeZoneId":"Asia/Qatar","DST":"3","GMT":"3"},{"TimeZoneId":"Asia/Qyzylorda","DST":"6","GMT":"6"},{"TimeZoneId":"Asia/Rangoon","DST":"6.5","GMT":"6.5"},{"TimeZoneId":"Asia/Riyadh","DST":"3","GMT":"3"},{"TimeZoneId":"Asia/Sakhalin","DST":"11","GMT":"10"},{"TimeZoneId":"Asia/Samarkand","DST":"5","GMT":"5"},{"TimeZoneId":"Asia/Seoul","DST":"9","GMT":"9"},{"TimeZoneId":"Asia/Shanghai","DST":"8","GMT":"8"},{"TimeZoneId":"Asia/Singapore","DST":"8","GMT":"8"},{"TimeZoneId":"Asia/Taipei","DST":"8","GMT":"8"},{"TimeZoneId":"Asia/Tashkent","DST":"5","GMT":"5"},{"TimeZoneId":"Asia/Tbilisi","DST":"4","GMT":"4"},{"TimeZoneId":"Asia/Tehran","DST":"4.5","GMT":"3.5"},{"TimeZoneId":"Asia/Thimphu","DST":"6","GMT":"6"},{"TimeZoneId":"Asia/Tokyo","DST":"9","GMT":"9"},{"TimeZoneId":"Asia/Ulaanbaatar","DST":"8","GMT":"8"},{"TimeZoneId":"Asia/Urumqi","DST":"8","GMT":"8"},{"TimeZoneId":"Asia/Vientiane","DST":"7","GMT":"7"},{"TimeZoneId":"Asia/Vladivostok","DST":"11","GMT":"10"},{"TimeZoneId":"Asia/Yakutsk","DST":"10","GMT":"9"},{"TimeZoneId":"Asia/Yekaterinburg","DST":"6","GMT":"5"},{"TimeZoneId":"Asia/Yerevan","DST":"5","GMT":"4"},{"TimeZoneId":"Atlantic/Azores","DST":"0","GMT":"-1"},{"TimeZoneId":"Atlantic/Bermuda","DST":"-3","GMT":"-4"},{"TimeZoneId":"Atlantic/Canary","DST":"1","GMT":"0"},{"TimeZoneId":"Atlantic/Cape Verde","DST":"-1","GMT":"-1"},{"TimeZoneId":"Atlantic/Faroe","DST":"1","GMT":"0"},{"TimeZoneId":"Atlantic/Madeira","DST":"1","GMT":"0"},{"TimeZoneId":"Atlantic/Reykjavik","DST":"0","GMT":"0"},{"TimeZoneId":"Atlantic/South Georgia","DST":"-2","GMT":"-2"},{"TimeZoneId":"Atlantic/St Helena","DST":"0","GMT":"0"},{"TimeZoneId":"Atlantic/Stanley","DST":"-4","GMT":"-3"},{"TimeZoneId":"Australia/Adelaide","DST":"9.5","GMT":"10.5"},{"TimeZoneId":"Australia/Brisbane","DST":"10","GMT":"10"},{"TimeZoneId":"Australia/Broken Hill","DST":"9.5","GMT":"10.5"},{"TimeZoneId":"Australia/Currie","DST":"10","GMT":"11"},{"TimeZoneId":"Australia/Darwin","DST":"9.5","GMT":"9.5"},{"TimeZoneId":"Australia/Eucla","DST":"8.75","GMT":"8.75"},{"TimeZoneId":"Australia/Hobart","DST":"10","GMT":"11"},{"TimeZoneId":"Australia/Lindeman","DST":"10","GMT":"10"},{"TimeZoneId":"Australia/Lord Howe","DST":"10.5","GMT":"11"},{"TimeZoneId":"Australia/Melbourne","DST":"10","GMT":"11"},{"TimeZoneId":"Australia/Perth","DST":"8","GMT":"8"},{"TimeZoneId":"Australia/Sydney","DST":"10","GMT":"11"},{"TimeZoneId":"Europe/Amsterdam","DST":"2","GMT":"1"},{"TimeZoneId":"Europe/Andorra","DST":"2","GMT":"1"},{"TimeZoneId":"Europe/Athens","DST":"3","GMT":"2"},{"TimeZoneId":"Europe/Belgrade","DST":"2","GMT":"1"},{"TimeZoneId":"Europe/Berlin","DST":"2","GMT":"1"},{"TimeZoneId":"Europe/Bratislava","DST":"2","GMT":"1"},{"TimeZoneId":"Europe/Brussels","DST":"2","GMT":"1"},{"TimeZoneId":"Europe/Bucharest","DST":"3","GMT":"2"},{"TimeZoneId":"Europe/Budapest","DST":"2","GMT":"1"},{"TimeZoneId":"Europe/Chisinau","DST":"3","GMT":"2"},{"TimeZoneId":"Europe/Copenhagen","DST":"2","GMT":"1"},{"TimeZoneId":"Europe/Dublin","DST":"1","GMT":"0"},{"TimeZoneId":"Europe/Gibraltar","DST":"2","GMT":"1"},{"TimeZoneId":"Europe/Guernsey","DST":"1","GMT":"0"},{"TimeZoneId":"Europe/Helsinki","DST":"3","GMT":"2"},{"TimeZoneId":"Europe/Isle of Man","DST":"1","GMT":"0"},{"TimeZoneId":"Europe/Istanbul","DST":"3","GMT":"2"},{"TimeZoneId":"Europe/Jersey","DST":"1","GMT":"0"},{"TimeZoneId":"Europe/Kaliningrad","DST":"3","GMT":"2"},{"TimeZoneId":"Europe/Kiev","DST":"3","GMT":"2"},{"TimeZoneId":"Europe/Lisbon","DST":"1","GMT":"0"},{"TimeZoneId":"Europe/Ljubljana","DST":"2","GMT":"1"},{"TimeZoneId":"Europe/London","DST":"1","GMT":"0"},{"TimeZoneId":"Europe/Luxembourg","DST":"2","GMT":"1"},{"TimeZoneId":"Europe/Madrid","DST":"2","GMT":"1"},{"TimeZoneId":"Europe/Malta","DST":"2","GMT":"1"},{"TimeZoneId":"Europe/Mariehamn","DST":"3","GMT":"2"},{"TimeZoneId":"Europe/Minsk","DST":"3","GMT":"2"},{"TimeZoneId":"Europe/Monaco","DST":"2","GMT":"1"},{"TimeZoneId":"Europe/Moscow","DST":"4","GMT":"3"},{"TimeZoneId":"Europe/Oslo","DST":"2","GMT":"1"},{"TimeZoneId":"Europe/Paris","DST":"2","GMT":"1"},{"TimeZoneId":"Europe/Podgorica","DST":"2","GMT":"1"},{"TimeZoneId":"Europe/Prague","DST":"2","GMT":"1"},{"TimeZoneId":"Europe/Riga","DST":"3","GMT":"2"},{"TimeZoneId":"Europe/Rome","DST":"2","GMT":"1"},{"TimeZoneId":"Europe/Samara","DST":"5","GMT":"4"},{"TimeZoneId":"Europe/San Marino","DST":"2","GMT":"1"},{"TimeZoneId":"Europe/Sarajevo","DST":"2","GMT":"1"},{"TimeZoneId":"Europe/Simferopol","DST":"3","GMT":"2"},{"TimeZoneId":"Europe/Skopje","DST":"2","GMT":"1"},{"TimeZoneId":"Europe/Sofia","DST":"3","GMT":"2"},{"TimeZoneId":"Europe/Stockholm","DST":"2","GMT":"1"},{"TimeZoneId":"Europe/Tallinn","DST":"3","GMT":"2"},{"TimeZoneId":"Europe/Tirane","DST":"2","GMT":"1"},{"TimeZoneId":"Europe/Uzhgorod","DST":"3","GMT":"2"},{"TimeZoneId":"Europe/Vaduz","DST":"2","GMT":"1"},{"TimeZoneId":"Europe/Vatican","DST":"2","GMT":"1"},{"TimeZoneId":"Europe/Vienna","DST":"2","GMT":"1"},{"TimeZoneId":"Europe/Vilnius","DST":"3","GMT":"2"},{"TimeZoneId":"Europe/Volgograd","DST":"4","GMT":"3"},{"TimeZoneId":"Europe/Warsaw","DST":"2","GMT":"1"},{"TimeZoneId":"Europe/Zagreb","DST":"2","GMT":"1"},{"TimeZoneId":"Europe/Zaporozhye","DST":"3","GMT":"2"},{"TimeZoneId":"Europe/Zurich","DST":"2","GMT":"1"},{"TimeZoneId":"Indian/Antananarivo","DST":"3","GMT":"3"},{"TimeZoneId":"Indian/Chagos","DST":"6","GMT":"6"},{"TimeZoneId":"Indian/Christmas","DST":"7","GMT":"7"},{"TimeZoneId":"Indian/Cocos","DST":"6.5","GMT":"6.5"},{"TimeZoneId":"Indian/Comoro","DST":"3","GMT":"3"},{"TimeZoneId":"Indian/Kerguelen","DST":"5","GMT":"5"},{"TimeZoneId":"Indian/Mahe","DST":"4","GMT":"4"},{"TimeZoneId":"Indian/Maldives","DST":"5","GMT":"5"},{"TimeZoneId":"Indian/Mauritius","DST":"4","GMT":"4"},{"TimeZoneId":"Indian/Mayotte","DST":"3","GMT":"3"},{"TimeZoneId":"Indian/Reunion","DST":"4","GMT":"4"},{"TimeZoneId":"Pacific/Apia","DST":"-11","GMT":"-10"},{"TimeZoneId":"Pacific/Auckland","DST":"12","GMT":"13"},{"TimeZoneId":"Pacific/Chatham","DST":"12.75","GMT":"13.75"},{"TimeZoneId":"Pacific/Easter","DST":"-6","GMT":"-5"},{"TimeZoneId":"Pacific/Efate","DST":"11","GMT":"11"},{"TimeZoneId":"Pacific/Enderbury","DST":"13","GMT":"13"},{"TimeZoneId":"Pacific/Fakaofo","DST":"-10","GMT":"-10"},{"TimeZoneId":"Pacific/Fiji","DST":"12","GMT":"13"},{"TimeZoneId":"Pacific/Funafuti","DST":"12","GMT":"12"},{"TimeZoneId":"Pacific/Galapagos","DST":"-6","GMT":"-6"},{"TimeZoneId":"Pacific/Gambier","DST":"-9","GMT":"-9"},{"TimeZoneId":"Pacific/Guadalcanal","DST":"11","GMT":"11"},{"TimeZoneId":"Pacific/Guam","DST":"10","GMT":"10"},{"TimeZoneId":"Pacific/Honolulu","DST":"-10","GMT":"-10"},{"TimeZoneId":"Pacific/Johnston","DST":"-10","GMT":"-10"},{"TimeZoneId":"Pacific/Kiritimati","DST":"14","GMT":"14"},{"TimeZoneId":"Pacific/Kosrae","DST":"11","GMT":"11"},{"TimeZoneId":"Pacific/Kwajalein","DST":"12","GMT":"12"},{"TimeZoneId":"Pacific/Majuro","DST":"12","GMT":"12"},{"TimeZoneId":"Pacific/Marquesas","DST":"-9.5","GMT":"-9.5"},{"TimeZoneId":"Pacific/Midway","DST":"-11","GMT":"-11"},{"TimeZoneId":"Pacific/Nauru","DST":"12","GMT":"12"},{"TimeZoneId":"Pacific/Niue","DST":"-11","GMT":"-11"},{"TimeZoneId":"Pacific/Norfolk","DST":"11.5","GMT":"11.5"},{"TimeZoneId":"Pacific/Noumea","DST":"11","GMT":"11"},{"TimeZoneId":"Pacific/Pago Pago","DST":"-11","GMT":"-11"},{"TimeZoneId":"Pacific/Palau","DST":"9","GMT":"9"},{"TimeZoneId":"Pacific/Pitcairn","DST":"-8","GMT":"-8"},{"TimeZoneId":"Pacific/Ponape","DST":"11","GMT":"11"},{"TimeZoneId":"Pacific/Port Moresby","DST":"10","GMT":"10"},{"TimeZoneId":"Pacific/Rarotonga","DST":"-10","GMT":"-10"},{"TimeZoneId":"Pacific/Saipan","DST":"10","GMT":"10"},{"TimeZoneId":"Pacific/Tahiti","DST":"-10","GMT":"-10"},{"TimeZoneId":"Pacific/Tarawa","DST":"12","GMT":"12"},{"TimeZoneId":"Pacific/Tongatapu","DST":"13","GMT":"13"},{"TimeZoneId":"Pacific/Truk","DST":"10","GMT":"10"},{"TimeZoneId":"Pacific/Wake","DST":"12","GMT":"12"},{"TimeZoneId":"Pacific/Wallis","DST":"12","GMT":"12"}]}';
olson = JSON.parse(olson);
var olsonKeys = Object.keys(olson);

Having one the timezone and its time lag, you can use this function:

function gerarData(a, b) {
    var match = {};
    for (var i = 0; i < olson.result.length; i++) {
        if (olson.result[i].TimeZoneId == b) match = olson.result[i];
    }

    var clientOffset = new Date().getTimezoneOffset() / 60;
    var timeZoneDSTOffset = parseInt(match.GMT);
    var offsetCorrection = (timeZoneDSTOffset - 3) * 60 * 60 * 1000; // *1000 porque o javascript trabalha em milisegundos
    var date = new Date(a * 1000 + offsetCorrection); 

    // função para corrigir numero de digitos para ter sempre 2
    function digitos(n) {
        return n > 9 ? "" + n : "0" + n;
    }
    // formatar a data
    var formatedDate = date.getFullYear() + '/' + 
        digitos(date.getMonth() + 1) + '/' +  // +1 porque o getMonth() começa em zero
        digitos(date.getDate()) + ' ' + 
        digitos(date.getHours()) + ':' + 
        digitos(date.getMinutes()) + ':' + 
        digitos(date.getSeconds());

    return formatedDate;
}

Demo

So on my demo:

gerarData(1357041600, "Europe/London");     // dá "2013/01/01 10:00:00"
gerarData(1372680000, "America/Sao Paulo"); // dá "2013/07/01 09:00:00"

Note that this suggestion of mine does not accept ã and other characters, but this you can fix with this question/answer.

This result is much simpler in PHP, you could create an ajax request and go fetch this value, but if understood well that’s what you do not want.

  • and this variable olson ? where it is declared?

  • humm, it’s in the fiddle, but I suggested an issue put the time zones (olson) in your reply, because it would be the right

  • 1

    @Pauloroberto, good idea to put the Olson object here. Thank you!

3

With native Javascript, the only way is to use a solution similar to what is suggested in Sergio’s response.

But if you can use a library, I suggest the Moment js.. To work with time zones, you also need to have the Moment Timezone.

One advantage of using this library is that time zone information changes all the time and it is difficult to keep your code abreast of all the rules (for example, in Brazil the summer time rule has recently changed). In the documentation explains how to keep data up to date.

With this library, the code is very easy:

function f(t, s) {
    return moment.tz(t * 1000, s).format('YYYY/MM/DD HH:mm:ss');
}

I had to multiply t for 1000 because the values you specified are in seconds, but the API works with milliseconds. To use:

function f(timestamp, timezone) {
    return moment.tz(timestamp * 1000, timezone).format('YYYY/MM/DD HH:mm:ss');
}

console.log(f(1357041600, "America/Sao_Paulo")); // 2013/01/01 10:00:00
console.log(f(1372680000, "America/Sao_Paulo")); // 2013/07/01 09:00:00
<script src="https://momentjs.com/downloads/moment.min.js"></script>
<script src="https://momentjs.com/downloads/moment-timezone-with-data.min.js"></script>

Note that the second parameter is America/Sao_Paulo: without accentuation and with a _ instead of space. That’s because timezone names follow the standard of the IANA.

If you want, you can adapt the code to accept more names, but always mapping to a valid name. To know all available names, use the function moment.tz.names(), that returns an array with all names. Ex:

function f(timestamp, timezone) {
    // se o timezone não existe, usa algum valor default
    if (! moment.tz.names().includes(timezone)) {
        timezone = 'America/Sao_Paulo';
    }
    return moment.tz(timestamp * 1000, timezone).format('YYYY/MM/DD HH:mm:ss');
}

console.log(f(1357041600, "America/Sao_Paulo")); // 2013/01/01 10:00:00
console.log(f(1372680000, "timezone invalido")); // 2013/07/01 09:00:00
<script src="https://momentjs.com/downloads/moment.min.js"></script>
<script src="https://momentjs.com/downloads/moment-timezone-with-data.min.js"></script>

1

You would have to periodically download the historical timezone list at http://www.iana.org/time-zones, on your server, interpret this document to a more Javascript palatable format (JSON?) and load this document at the time of executing its "f" function, so that it can search for Timezone by name.

Java seems to have this information too, directly via API, so another output would be to load a Java applet that your Javascript code could then query. This if you can force clients to use Java in the browser and the best solution (solve on the server side) is not feasible.

Note that your "f" function does not need to communicate with the each call. Just once to load the table, and if the table is converted to JSON, it can be loaded as an extra JS source on the page.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.