What method can I use to shorten the time zone abbreviation in all browsers?

In Chrome, Safari, and Firefox, I use this call:

var date = new Date();
date.toTimeString().match(/\((.*)\)/)

But this does not work in IE 9 because IE 9 returns data without parentheses. And this does not work in IE 11 because IE 11 does not return the abbreviation, but instead returns something like (standard Pacific time).

Example output IE9-IE10:

"15:38:43 PST"

Example output from IE11 and Edge:

"15:36:07 GMT-0800 (Pacific Standard Time)"
+4
source share
3 answers

Some background:

  • , , - ( IANA)
  • , , , . IE Chrome " E. Australia", , " ".
  • , . EST 3 .
  • , toTimeString, , , , , (. ).

, , Date.prototype.getTimezoneOffset , , (, jsTimezoneDetect), IANA.

:

- .

, IANA , , .

+3

MomentJS :

moment.tz([2012, 0], 'America/New_York').format('z');    // EST
moment.tz([2012, 5], 'America/New_York').format('z');    // EDT
moment.tz([2012, 0], 'America/Los_Angeles').format('z'); // PST
moment.tz([2012, 5], 'America/Los_Angeles').format('z'); // PDT
+2

Try

Intl.DateTimeFormat().resolvedOptions().timeZone

Waring: it is not compatible with all browser versions

0
source

Source: https://habr.com/ru/post/1615666/


All Articles