Last active
July 4, 2021 02:02
-
-
Save andrejtest-675/61bee6d4c13a72e5c9b0f6597371ee91 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Also I know it's better to find some library for this task but if someone is okay with using a pure javascript solution, here's an example snippet. Please note timezone names are hard-coded but I don't expect the names change (the names, offset could change). | |
// It is a response to https://gist.github.com/redoPop/3915761 | |
/* | |
It has 4 fallbacks, | |
1. first it tries to format the date as a string `${timezone}`, | |
and extract the timezone name from the braces at the end, | |
and look up the abbr in the dict, | |
2. then, if it's not in the dict, it just prints the part within ( and ), | |
3. if the date in text representation is not formatted with ( and ) at the end, | |
it will use the window.Intl object and print the timezone name from there | |
"Europe/Moscow" is an example what it gives. It's not an abbr but it's better than nothing | |
4. if all 3 conditions mentioned above fail, it will print GMT+offset | |
*/ | |
const TIMEZONE_ABBR_DICT = [ | |
{abbr:"A", longname:"Alpha Time Zone"}, | |
{abbr:"ACDT", longname:"Australian Central Daylight Time"}, | |
{abbr:"ACST", longname:"Australian Central Standard Time"}, | |
{abbr:"ACT", longname:"Acre Time"}, | |
{abbr:"ACT", longname:"Australian Central Time"}, | |
{abbr:"ACWST", longname:"Australian Central Western Standard Time"}, | |
{abbr:"ADT", longname:"Arabia Daylight Time"}, | |
{abbr:"ADT", longname:"Atlantic Daylight Time"}, | |
{abbr:"AEDT", longname:"Australian Eastern Daylight Time"}, | |
{abbr:"AEST", longname:"Australian Eastern Standard Time"}, | |
{abbr:"AET", longname:"Australian Eastern Time"}, | |
{abbr:"AFT", longname:"Afghanistan Time"}, | |
{abbr:"AKDT", longname:"Alaska Daylight Time"}, | |
{abbr:"AKST", longname:"Alaska Standard Time"}, | |
{abbr:"ALMT", longname:"Alma-Ata Time"}, | |
{abbr:"AMST", longname:"Amazon Summer Time"}, | |
{abbr:"AMST", longname:"Armenia Summer Time"}, | |
{abbr:"AMT", longname:"Amazon Time"}, | |
{abbr:"AMT", longname:"Armenia Time"}, | |
{abbr:"ANAST", longname:"Anadyr Summer Time"}, | |
{abbr:"ANAT", longname:"Anadyr Time"}, | |
{abbr:"AQTT", longname:"Aqtobe Time"}, | |
{abbr:"ART", longname:"Argentina Time"}, | |
{abbr:"AST", longname:"Arabia Standard Time"}, | |
{abbr:"AST", longname:"Atlantic Standard Time"}, | |
{abbr:"AT", longname:"Atlantic Time"}, | |
{abbr:"AWDT", longname:"Australian Western Daylight Time"}, | |
{abbr:"AWST", longname:"Australian Western Standard Time"}, | |
{abbr:"AZOST", longname:"Azores Summer Time"}, | |
{abbr:"AZOT", longname:"Azores Time"}, | |
{abbr:"AZST", longname:"Azerbaijan Summer Time"}, | |
{abbr:"AZT", longname:"Azerbaijan Time"}, | |
{abbr:"AoE", longname:"Anywhere on Earth"}, | |
{abbr:"B", longname:"Bravo Time Zone"}, | |
{abbr:"BNT", longname:"Brunei Darussalam Time"}, | |
{abbr:"BOT", longname:"Bolivia Time"}, | |
{abbr:"BRST", longname:"Brasília Summer Time"}, | |
{abbr:"BRT", longname:"Brasília Time"}, | |
{abbr:"BST", longname:"Bangladesh Standard Time"}, | |
{abbr:"BST", longname:"Bougainville Standard Time"}, | |
{abbr:"BST", longname:"British Summer Time"}, | |
{abbr:"BTT", longname:"Bhutan Time"}, | |
{abbr:"C", longname:"Charlie Time Zone"}, | |
{abbr:"CAST", longname:"Casey Time"}, | |
{abbr:"CAT", longname:"Central Africa Time"}, | |
{abbr:"CCT", longname:"Cocos Islands Time"}, | |
{abbr:"CDT", longname:"Central Daylight Time"}, | |
{abbr:"CDT", longname:"Cuba Daylight Time"}, | |
{abbr:"CEST", longname:"Central European Summer Time"}, | |
{abbr:"CET", longname:"Central European Time"}, | |
{abbr:"CHADT", longname:"Chatham Island Daylight Time"}, | |
{abbr:"CHAST", longname:"Chatham Island Standard Time"}, | |
{abbr:"CHOST", longname:"Choibalsan Summer Time"}, | |
{abbr:"CHOT", longname:"Choibalsan Time"}, | |
{abbr:"CHUT", longname:"Chuuk Time"}, | |
{abbr:"CIDST", longname:"Cayman Islands Daylight Saving Time"}, | |
{abbr:"CIST", longname:"Cayman Islands Standard Time"}, | |
{abbr:"CKT", longname:"Cook Island Time"}, | |
{abbr:"CLST", longname:"Chile Summer Time"}, | |
{abbr:"CLT", longname:"Chile Standard Time"}, | |
{abbr:"COT", longname:"Colombia Time"}, | |
{abbr:"CST", longname:"Central Standard Time"}, | |
{abbr:"CST", longname:"China Standard Time"}, | |
{abbr:"CST", longname:"Cuba Standard Time"}, | |
{abbr:"CT", longname:"Central Time"}, | |
{abbr:"CVT", longname:"Cape Verde Time"}, | |
{abbr:"CXT", longname:"Christmas Island Time"}, | |
{abbr:"ChST", longname:"Chamorro Standard Time"}, | |
{abbr:"D", longname:"Delta Time Zone"}, | |
{abbr:"DAVT", longname:"Davis Time"}, | |
{abbr:"DDUT", longname:"Dumont-d'Urville Time"}, | |
{abbr:"E", longname:"Echo Time Zone"}, | |
{abbr:"EASST", longname:"Easter Island Summer Time"}, | |
{abbr:"EAST", longname:"Easter Island Standard Time"}, | |
{abbr:"EAT", longname:"Eastern Africa Time"}, | |
{abbr:"ECT", longname:"Ecuador Time"}, | |
{abbr:"EDT", longname:"Eastern Daylight Time"}, | |
{abbr:"EEST", longname:"Eastern European Summer Time"}, | |
{abbr:"EET", longname:"Eastern European Time"}, | |
{abbr:"EGST", longname:"Eastern Greenland Summer Time"}, | |
{abbr:"EGT", longname:"East Greenland Time"}, | |
{abbr:"EST", longname:"Eastern Standard Time"}, | |
{abbr:"ET", longname:"Eastern Time"}, | |
{abbr:"F", longname:"Foxtrot Time Zone"}, | |
{abbr:"FET", longname:"Further-Eastern European Time"}, | |
{abbr:"FJST", longname:"Fiji Summer Time"}, | |
{abbr:"FJT", longname:"Fiji Time"}, | |
{abbr:"FKST", longname:"Falkland Islands Summer Time"}, | |
{abbr:"FKT", longname:"Falkland Island Time"}, | |
{abbr:"FNT", longname:"Fernando de Noronha Time"}, | |
{abbr:"G", longname:"Golf Time Zone"}, | |
{abbr:"GALT", longname:"Galapagos Time"}, | |
{abbr:"GAMT", longname:"Gambier Time"}, | |
{abbr:"GET", longname:"Georgia Standard Time"}, | |
{abbr:"GFT", longname:"French Guiana Time"}, | |
{abbr:"GILT", longname:"Gilbert Island Time"}, | |
{abbr:"GMT", longname:"Greenwich Mean Time"}, | |
{abbr:"GST", longname:"Gulf Standard Time"}, | |
{abbr:"GST", longname:"South Georgia Time"}, | |
{abbr:"GYT", longname:"Guyana Time"}, | |
{abbr:"H", longname:"Hotel Time Zone"}, | |
{abbr:"HDT", longname:"Hawaii-Aleutian Daylight Time"}, | |
{abbr:"HKT", longname:"Hong Kong Time"}, | |
{abbr:"HOVST", longname:"Hovd Summer Time"}, | |
{abbr:"HOVT", longname:"Hovd Time"}, | |
{abbr:"HST", longname:"Hawaii Standard Time"}, | |
{abbr:"I", longname:"India Time Zone"}, | |
{abbr:"ICT", longname:"Indochina Time"}, | |
{abbr:"IDT", longname:"Israel Daylight Time"}, | |
{abbr:"IOT", longname:"Indian Chagos Time"}, | |
{abbr:"IRDT", longname:"Iran Daylight Time"}, | |
{abbr:"IRKST", longname:"Irkutsk Summer Time"}, | |
{abbr:"IRKT", longname:"Irkutsk Time"}, | |
{abbr:"IRST", longname:"Iran Standard Time"}, | |
{abbr:"IST", longname:"India Standard Time"}, | |
{abbr:"IST", longname:"Irish Standard Time"}, | |
{abbr:"IST", longname:"Israel Standard Time"}, | |
{abbr:"JST", longname:"Japan Standard Time"}, | |
{abbr:"K", longname:"Kilo Time Zone"}, | |
{abbr:"KGT", longname:"Kyrgyzstan Time"}, | |
{abbr:"KOST", longname:"Kosrae Time"}, | |
{abbr:"KRAST", longname:"Krasnoyarsk Summer Time"}, | |
{abbr:"KRAT", longname:"Krasnoyarsk Time"}, | |
{abbr:"KST", longname:"Korea Standard Time"}, | |
{abbr:"KUYT", longname:"Kuybyshev Time"}, | |
{abbr:"L", longname:"Lima Time Zone"}, | |
{abbr:"LHDT", longname:"Lord Howe Daylight Time"}, | |
{abbr:"LHST", longname:"Lord Howe Standard Time"}, | |
{abbr:"LINT", longname:"Line Islands Time"}, | |
{abbr:"M", longname:"Mike Time Zone"}, | |
{abbr:"MAGST", longname:"Magadan Summer Time"}, | |
{abbr:"MAGT", longname:"Magadan Time"}, | |
{abbr:"MART", longname:"Marquesas Time"}, | |
{abbr:"MAWT", longname:"Mawson Time"}, | |
{abbr:"MDT", longname:"Mountain Daylight Time"}, | |
{abbr:"MHT", longname:"Marshall Islands Time"}, | |
{abbr:"MMT", longname:"Myanmar Time"}, | |
{abbr:"MSD", longname:"Moscow Daylight Time"}, | |
{abbr:"MSK", longname:"Moscow Standard Time"}, | |
{abbr:"MST", longname:"Mountain Standard Time"}, | |
{abbr:"MT", longname:"Mountain Time"}, | |
{abbr:"MUT", longname:"Mauritius Time"}, | |
{abbr:"MVT", longname:"Maldives Time"}, | |
{abbr:"MYT", longname:"Malaysia Time"}, | |
{abbr:"N", longname:"November Time Zone"}, | |
{abbr:"NCT", longname:"New Caledonia Time"}, | |
{abbr:"NDT", longname:"Newfoundland Daylight Time"}, | |
{abbr:"NFDT", longname:"Norfolk Daylight Time"}, | |
{abbr:"NFT", longname:"Norfolk Time"}, | |
{abbr:"NOVST", longname:"Novosibirsk Summer Time"}, | |
{abbr:"NOVT", longname:"Novosibirsk Time"}, | |
{abbr:"NPT", longname:"Nepal Time"}, | |
{abbr:"NRT", longname:"Nauru Time"}, | |
{abbr:"NST", longname:"Newfoundland Standard Time"}, | |
{abbr:"NUT", longname:"Niue Time"}, | |
{abbr:"NZDT", longname:"New Zealand Daylight Time"}, | |
{abbr:"NZST", longname:"New Zealand Standard Time"}, | |
{abbr:"O", longname:"Oscar Time Zone"}, | |
{abbr:"OMSST", longname:"Omsk Summer Time"}, | |
{abbr:"OMST", longname:"Omsk Standard Time"}, | |
{abbr:"ORAT", longname:"Oral Time"}, | |
{abbr:"P", longname:"Papa Time Zone"}, | |
{abbr:"PDT", longname:"Pacific Daylight Time"}, | |
{abbr:"PET", longname:"Peru Time"}, | |
{abbr:"PETST", longname:"Kamchatka Summer Time"}, | |
{abbr:"PETT", longname:"Kamchatka Time"}, | |
{abbr:"PGT", longname:"Papua New Guinea Time"}, | |
{abbr:"PHOT", longname:"Phoenix Island Time"}, | |
{abbr:"PHT", longname:"Philippine Time"}, | |
{abbr:"PKT", longname:"Pakistan Standard Time"}, | |
{abbr:"PMDT", longname:"Pierre & Miquelon Daylight Time"}, | |
{abbr:"PMST", longname:"Pierre & Miquelon Standard Time"}, | |
{abbr:"PONT", longname:"Pohnpei Standard Time"}, | |
{abbr:"PST", longname:"Pacific Standard Time"}, | |
{abbr:"PST", longname:"Pitcairn Standard Time"}, | |
{abbr:"PT", longname:"Pacific Time"}, | |
{abbr:"PWT", longname:"Palau Time"}, | |
{abbr:"PYST", longname:"Paraguay Summer Time"}, | |
{abbr:"PYT", longname:"Paraguay Time"}, | |
{abbr:"PYT", longname:"Pyongyang Time"}, | |
{abbr:"Q", longname:"Quebec Time Zone"}, | |
{abbr:"QYZT", longname:"Qyzylorda Time"}, | |
{abbr:"R", longname:"Romeo Time Zone"}, | |
{abbr:"RET", longname:"Reunion Time"}, | |
{abbr:"ROTT", longname:"Rothera Time"}, | |
{abbr:"S", longname:"Sierra Time Zone"}, | |
{abbr:"SAKT", longname:"Sakhalin Time"}, | |
{abbr:"SAMT", longname:"Samara Time"}, | |
{abbr:"SAST", longname:"South Africa Standard Time"}, | |
{abbr:"SBT", longname:"Solomon Islands Time"}, | |
{abbr:"SCT", longname:"Seychelles Time"}, | |
{abbr:"SGT", longname:"Singapore Time"}, | |
{abbr:"SRET", longname:"Srednekolymsk Time"}, | |
{abbr:"SRT", longname:"Suriname Time"}, | |
{abbr:"SST", longname:"Samoa Standard Time"}, | |
{abbr:"SYOT", longname:"Syowa Time"}, | |
{abbr:"T", longname:"Tango Time Zone"}, | |
{abbr:"TAHT", longname:"Tahiti Time"}, | |
{abbr:"TFT", longname:"French Southern and Antarctic Time"}, | |
{abbr:"TJT", longname:"Tajikistan Time"}, | |
{abbr:"TKT", longname:"Tokelau Time"}, | |
{abbr:"TLT", longname:"East Timor Time"}, | |
{abbr:"TMT", longname:"Turkmenistan Time"}, | |
{abbr:"TOST", longname:"Tonga Summer Time"}, | |
{abbr:"TOT", longname:"Tonga Time"}, | |
{abbr:"TRT", longname:"Turkey Time"}, | |
{abbr:"TVT", longname:"Tuvalu Time"}, | |
{abbr:"U", longname:"Uniform Time Zone"}, | |
{abbr:"ULAST", longname:"Ulaanbaatar Summer Time"}, | |
{abbr:"ULAT", longname:"Ulaanbaatar Time"}, | |
{abbr:"UTC", longname:"Coordinated Universal Time"}, | |
{abbr:"UYST", longname:"Uruguay Summer Time"}, | |
{abbr:"UYT", longname:"Uruguay Time"}, | |
{abbr:"UZT", longname:"Uzbekistan Time"}, | |
{abbr:"V", longname:"Victor Time Zone"}, | |
{abbr:"VET", longname:"Venezuelan Standard Time"}, | |
{abbr:"VLAST", longname:"Vladivostok Summer Time"}, | |
{abbr:"VLAT", longname:"Vladivostok Time"}, | |
{abbr:"VOST", longname:"Vostok Time"}, | |
{abbr:"VUT", longname:"Vanuatu Time"}, | |
{abbr:"W", longname:"Whiskey Time Zone"}, | |
{abbr:"WAKT", longname:"Wake Time"}, | |
{abbr:"WARST", longname:"Western Argentine Summer Time"}, | |
{abbr:"WAST", longname:"West Africa Summer Time"}, | |
{abbr:"WAT", longname:"West Africa Time"}, | |
{abbr:"WEST", longname:"Western European Summer Time"}, | |
{abbr:"WET", longname:"Western European Time"}, | |
{abbr:"WFT", longname:"Wallis and Futuna Time"}, | |
{abbr:"WGST", longname:"Western Greenland Summer Time"}, | |
{abbr:"WGT", longname:"West Greenland Time"}, | |
{abbr:"WIB", longname:"Western Indonesian Time"}, | |
{abbr:"WIT", longname:"Eastern Indonesian Time"}, | |
{abbr:"WITA", longname:"Central Indonesian Time"}, | |
{abbr:"WST", longname:"West Samoa Time"}, | |
{abbr:"WST", longname:"Western Sahara Summer Time"}, | |
{abbr:"WT", longname:"Western Sahara Standard Time"}, | |
{abbr:"X", longname:"X-ray Time Zone"}, | |
{abbr:"Y", longname:"Yankee Time Zone"}, | |
{abbr:"YAKST", longname:"Yakutsk Summer Time"}, | |
{abbr:"YAKT", longname:"Yakutsk Time"}, | |
{abbr:"YAPT", longname:"Yap Time"}, | |
{abbr:"YEKST", longname:"Yekaterinburg Summer Time"}, | |
{abbr:"YEKT", longname:"Yekaterinburg Time"}, | |
{abbr:"Z", longname:"Zulu Time Zone"}, | |
]; | |
const extractTimezoneAbbr = datetime => {datetime=datetime||new Date();const m=`${datetime}`.match(/\((.*?)\)\s*$/);if(m){const s=r=>r.replace(/[^\w]/ig,' ').replace(/\s+/ig,' ').replace(/^\s*/,'').replace(/\s*$/,'');const longstr=s(m[1]).toLowerCase();let found=null;TIMEZONE_ABBR_DICT.forEach(e=>{if(s(e.longname).toLowerCase()===longstr)found=e.abbr;});return found||longstr;}else{if(window.Intl)return `${new window.Intl.DateTimeFormat().resolvedOptions().timeZone}`;else return `GMT${datetime.getTimezoneOffset()<0?'':'+'}${datetime.getTimezoneOffset()/60}`;}}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment