Skip to content

Instantly share code, notes, and snippets.

@dimitrilahaye
Last active January 17, 2020 13:44
Show Gist options
  • Save dimitrilahaye/fe96ffd751b7c3b353ede0abcacf28c9 to your computer and use it in GitHub Desktop.
Save dimitrilahaye/fe96ffd751b7c3b353ede0abcacf28c9 to your computer and use it in GitHub Desktop.
Use a date string from a specific timezone and return a converted date object in local timezone

Jetlag function

This function takes a date string and its specific timezone and return a converted date object in client's local timezone

Example with a server based in Berlin:

// the string date "2020-01-15 15:25:55" is based on Berlin timezone
// jetLag will give us a Date instance in the right client's local timezone.
const localDate = jetLag("2020-01-15 15:25:55", "Berlin");
/**
* Returns a date object with time conversion.
* We assume that {serverDateString} is contained into the {serverZone} timezone (example 'Berlin').
* So we will convert this server's time in the client's timezone.
* @see https://www.techrepublic.com/article/convert-the-local-time-to-another-time-zone-with-this-javascript/
*/
function jetLag(serverDateString: string, serverZone: string): Date {
const serverTimeZone: ITimeZone | undefined = TimesZones.find((country) => country.zone === serverZone);
if (!serverTimeZone) throw `${serverZone} is missing or does not exist.`;
const d: Date = new Date(serverDateString),
utc: number = d.getTime() + (d.getTimezoneOffset() * 60000);
return new Date(utc + (3600000 * +serverTimeZone.offset));
}
// this part is made with the help of https://gist.github.com/themeteorchef/57622dc133973a1272c7
interface ITimeZone {
zone: "International Date Line West" | "Midway Island" | "Samoa" | "Hawaii" | "Alaska" | "America/Anchorage" | "America/Los_Angeles" | "Pacific Time (US & Canada)" | "Tijuana" | "America/Denver" | "America/Phoenix" | "Arizona" | "Chihuahua" | "Mazatlan" | "Mountain Time (US & Canada)" | "America/Chicago" | "America/Guatemala" | "Central America" | "Central Time (US & Canada)" | "Guadalajara" | "Mexico City" | "Monterrey" | "Saskatchewan" | "America/Bogota" | "America/New_York" | "Bogota" | "Eastern Time (US & Canada)" | "Indiana (East)" | "Lima" | "Quito" | "Caracas" | "Atlantic Time (Canada)" | "Georgetown" | "La Paz" | "Santiago" | "Newfoundland" | "America/Argentina/Buenos_Aires" | "America/Sao_Paulo" | "Brasilia" | "Buenos Aires" | "Greenland" | "Mid-Atlantic" | "Azores" | "Cape Verde Is." | "Casablanca" | "Dublin" | "Edinburgh" | "Europe/London" | "Lisbon" | "London" | "Monrovia" | "UTC" | "Africa/Lagos" | "Amsterdam" | "Belgrade" | "Berlin" | "Bern" | "Bratislava" | "Brussels" | "Budapest" | "Copenhagen" | "Europe/Berlin" | "Ljubljana" | "Madrid" | "Paris" | "Prague" | "Rome" | "Sarajevo" | "Skopje" | "Stockholm" | "Vienna" | "Warsaw" | "West Central Africa" | "Zagreb" | "Africa/Johannesburg" | "Athens" | "Bucharest" | "Cairo" | "Europe/Helsinki" | "Harare" | "Helsinki" | "Istanbul" | "Jerusalem" | "Kyiv" | "Pretoria" | "Riga" | "Sofia" | "Tallinn" | "Vilnius" | "Baghdad" | "Kuwait" | "Minsk" | "Nairobi" | "Riyadh" | "Tehran" | "Abu Dhabi" | "Asia/Dubai" | "Asia/Yerevan" | "Baku" | "Moscow" | "Muscat" | "St. Petersburg" | "Tbilisi" | "Volgograd" | "Yerevan" | "Kabul" | "Islamabad" | "Karachi" | "Tashkent" | "Asia/Kolkata" | "Chennai" | "Kolkata" | "Mumbai" | "New Delhi" | "Sri Jayawardenepura" | "Kathmandu" | "Almaty" | "Astana" | "Dhaka" | "Ekaterinburg" | "Rangoon" | "Asia/Jakarta" | "Bangkok" | "Hanoi" | "Jakarta" | "Novosibirsk" | "Asia/Shanghai" | "Beijing" | "Chongqing" | "Hong Kong" | "Krasnoyarsk" | "Kuala Lumpur" | "Perth" | "Singapore" | "Taipei" | "Ulaan Bataar" | "Urumqi" | "Asia/Tokyo" | "Irkutsk" | "Osaka" | "Sapporo" | "Seoul" | "Tokyo" | "Adelaide" | "Australia/Adelaide" | "Darwin" | "Australia/Brisbane" | "Australia/Sydney" | "Brisbane" | "Canberra" | "Guam" | "Hobart" | "Melbourne" | "Port Moresby" | "Sydney" | "Yakutsk" | "New Caledonia" | "Vladivostok" | "Auckland" | "Fiji" | "Kamchatka" | "Magadan" | "Marshall Is." | "Pacific/Auckland" | "Solomon Is." | "Wellington" | "Nuku'alofa";
offset: "-11" | "-10" | "-9" | "-8" | "-7" | "-6" | "-5" | "-4" | "-3" | "-2" | "-1" | "+0" | "+1" | "+2" | "+3" | "+4" | "+5" | "+6" | "+7" | "+8" | "+9" | "+10" | "+11" | "+12" | "+13";
}
const TimesZones: Array<ITimeZone> = [{"zone":"International Date Line West","offset":"-11"},{"zone":"Midway Island","offset":"-11"},{"zone":"Samoa","offset":"-11"},{"zone":"Hawaii","offset":"-10"},{"zone":"Alaska","offset":"-9"},{"zone":"America/Anchorage","offset":"-9"},{"zone":"America/Los_Angeles","offset":"-8"},{"zone":"Pacific Time (US & Canada)","offset":"-8"},{"zone":"Tijuana","offset":"-8"},{"zone":"America/Denver","offset":"-7"},{"zone":"America/Phoenix","offset":"-7"},{"zone":"Arizona","offset":"-7"},{"zone":"Chihuahua","offset":"-7"},{"zone":"Mazatlan","offset":"-7"},{"zone":"Mountain Time (US & Canada)","offset":"-7"},{"zone":"America/Chicago","offset":"-6"},{"zone":"America/Guatemala","offset":"-6"},{"zone":"Central America","offset":"-6"},{"zone":"Central Time (US & Canada)","offset":"-6"},{"zone":"Guadalajara","offset":"-6"},{"zone":"Mexico City","offset":"-6"},{"zone":"Monterrey","offset":"-6"},{"zone":"Saskatchewan","offset":"-6"},{"zone":"America/Bogota","offset":"-5"},{"zone":"America/New_York","offset":"-5"},{"zone":"Bogota","offset":"-5"},{"zone":"Eastern Time (US & Canada)","offset":"-5"},{"zone":"Indiana (East)","offset":"-5"},{"zone":"Lima","offset":"-5"},{"zone":"Quito","offset":"-5"},{"zone":"Caracas","offset":"-4"},{"zone":"Atlantic Time (Canada)","offset":"-4"},{"zone":"Georgetown","offset":"-4"},{"zone":"La Paz","offset":"-4"},{"zone":"Santiago","offset":"-4"},{"zone":"Newfoundland","offset":"-3"},{"zone":"America/Argentina/Buenos_Aires","offset":"-3"},{"zone":"America/Sao_Paulo","offset":"-3"},{"zone":"Brasilia","offset":"-3"},{"zone":"Buenos Aires","offset":"-3"},{"zone":"Greenland","offset":"-3"},{"zone":"Mid-Atlantic","offset":"-2"},{"zone":"Azores","offset":"-1"},{"zone":"Cape Verde Is.","offset":"-1"},{"zone":"Casablanca","offset":"+0"},{"zone":"Dublin","offset":"+0"},{"zone":"Edinburgh","offset":"+0"},{"zone":"Europe/London","offset":"+0"},{"zone":"Lisbon","offset":"+0"},{"zone":"London","offset":"+0"},{"zone":"Monrovia","offset":"+0"},{"zone":"UTC","offset":"+0"},{"zone":"Africa/Lagos","offset":"+1"},{"zone":"Amsterdam","offset":"+1"},{"zone":"Belgrade","offset":"+1"},{"zone":"Berlin","offset":"+1"},{"zone":"Bern","offset":"+1"},{"zone":"Bratislava","offset":"+1"},{"zone":"Brussels","offset":"+1"},{"zone":"Budapest","offset":"+1"},{"zone":"Copenhagen","offset":"+1"},{"zone":"Europe/Berlin","offset":"+1"},{"zone":"Ljubljana","offset":"+1"},{"zone":"Madrid","offset":"+1"},{"zone":"Paris","offset":"+1"},{"zone":"Prague","offset":"+1"},{"zone":"Rome","offset":"+1"},{"zone":"Sarajevo","offset":"+1"},{"zone":"Skopje","offset":"+1"},{"zone":"Stockholm","offset":"+1"},{"zone":"Vienna","offset":"+1"},{"zone":"Warsaw","offset":"+1"},{"zone":"West Central Africa","offset":"+1"},{"zone":"Zagreb","offset":"+1"},{"zone":"Africa/Johannesburg","offset":"+2"},{"zone":"Athens","offset":"+2"},{"zone":"Bucharest","offset":"+2"},{"zone":"Cairo","offset":"+2"},{"zone":"Europe/Helsinki","offset":"+2"},{"zone":"Harare","offset":"+2"},{"zone":"Helsinki","offset":"+2"},{"zone":"Istanbul","offset":"+2"},{"zone":"Jerusalem","offset":"+2"},{"zone":"Kyiv","offset":"+2"},{"zone":"Pretoria","offset":"+2"},{"zone":"Riga","offset":"+2"},{"zone":"Sofia","offset":"+2"},{"zone":"Tallinn","offset":"+2"},{"zone":"Vilnius","offset":"+2"},{"zone":"Baghdad","offset":"+3"},{"zone":"Kuwait","offset":"+3"},{"zone":"Minsk","offset":"+3"},{"zone":"Nairobi","offset":"+3"},{"zone":"Riyadh","offset":"+3"},{"zone":"Tehran","offset":"+3"},{"zone":"Abu Dhabi","offset":"+4"},{"zone":"Asia/Dubai","offset":"+4"},{"zone":"Asia/Yerevan","offset":"+4"},{"zone":"Baku","offset":"+4"},{"zone":"Moscow","offset":"+4"},{"zone":"Muscat","offset":"+4"},{"zone":"St. Petersburg","offset":"+4"},{"zone":"Tbilisi","offset":"+4"},{"zone":"Volgograd","offset":"+4"},{"zone":"Yerevan","offset":"+4"},{"zone":"Kabul","offset":"+4"},{"zone":"Islamabad","offset":"+5"},{"zone":"Karachi","offset":"+5"},{"zone":"Tashkent","offset":"+5"},{"zone":"Asia/Kolkata","offset":"+5"},{"zone":"Chennai","offset":"+5"},{"zone":"Kolkata","offset":"+5"},{"zone":"Mumbai","offset":"+5"},{"zone":"New Delhi","offset":"+5"},{"zone":"Sri Jayawardenepura","offset":"+5"},{"zone":"Kathmandu","offset":"+5"},{"zone":"Almaty","offset":"+6"},{"zone":"Astana","offset":"+6"},{"zone":"Dhaka","offset":"+6"},{"zone":"Ekaterinburg","offset":"+6"},{"zone":"Rangoon","offset":"+6"},{"zone":"Asia/Jakarta","offset":"+7"},{"zone":"Bangkok","offset":"+7"},{"zone":"Hanoi","offset":"+7"},{"zone":"Jakarta","offset":"+7"},{"zone":"Novosibirsk","offset":"+7"},{"zone":"Asia/Shanghai","offset":"+8"},{"zone":"Beijing","offset":"+8"},{"zone":"Chongqing","offset":"+8"},{"zone":"Hong Kong","offset":"+8"},{"zone":"Krasnoyarsk","offset":"+8"},{"zone":"Kuala Lumpur","offset":"+8"},{"zone":"Perth","offset":"+8"},{"zone":"Singapore","offset":"+8"},{"zone":"Taipei","offset":"+8"},{"zone":"Ulaan Bataar","offset":"+8"},{"zone":"Urumqi","offset":"+8"},{"zone":"Asia/Tokyo","offset":"+9"},{"zone":"Irkutsk","offset":"+9"},{"zone":"Osaka","offset":"+9"},{"zone":"Sapporo","offset":"+9"},{"zone":"Seoul","offset":"+9"},{"zone":"Tokyo","offset":"+9"},{"zone":"Adelaide","offset":"+9"},{"zone":"Australia/Adelaide","offset":"+9"},{"zone":"Darwin","offset":"+9"},{"zone":"Australia/Brisbane","offset":"+10"},{"zone":"Australia/Sydney","offset":"+10"},{"zone":"Brisbane","offset":"+10"},{"zone":"Canberra","offset":"+10"},{"zone":"Guam","offset":"+10"},{"zone":"Hobart","offset":"+10"},{"zone":"Melbourne","offset":"+10"},{"zone":"Port Moresby","offset":"+10"},{"zone":"Sydney","offset":"+10"},{"zone":"Yakutsk","offset":"+10"},{"zone":"New Caledonia","offset":"+11"},{"zone":"Vladivostok","offset":"+11"},{"zone":"Auckland","offset":"+12"},{"zone":"Fiji","offset":"+12"},{"zone":"Kamchatka","offset":"+12"},{"zone":"Magadan","offset":"+12"},{"zone":"Marshall Is.","offset":"+12"},{"zone":"Pacific/Auckland","offset":"+12"},{"zone":"Solomon Is.","offset":"+12"},{"zone":"Wellington","offset":"+12"},{"zone":"Nuku'alofa","offset":"+13"}];