showing results for - "js new timexone name"
Hajar
16 Oct 2017
1function getTimezoneName() {
2  const today = new Date();
3  const short = today.toLocaleDateString(undefined);
4  const full = today.toLocaleDateString(undefined, { timeZoneName: 'long' });
5
6  // Trying to remove date from the string in a locale-agnostic way
7  const shortIndex = full.indexOf(short);
8  if (shortIndex >= 0) {
9    const trimmed = full.substring(0, shortIndex) + full.substring(shortIndex + short.length);
10    
11    // by this time `trimmed` should be the timezone's name with some punctuation -
12    // trim it from both sides
13    return trimmed.replace(/^[\s,.\-:;]+|[\s,.\-:;]+$/g, '');
14
15  } else {
16    // in some magic case when short representation of date is not present in the long one, just return the long one as a fallback, since it should contain the timezone's name
17    return full;
18  }
19}
20
21console.log(getTimezoneName());
queries leading to this page
js new timexone namejs new timexone name