4

In Chrome, Safari, and Firefox I'm using a call like this:

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

But this doesn't work in IE 9 because IE 9 returns it's data without the parenthesis. And this doesn't work in IE 11 because IE 11 doesn't return the abbreviation but instead returns something like (Pacific Standard Time).

Example IE9-IE10 output:

"15:38:43 PST"

Example IE11 and Edge output:

"15:36:07 GMT-0800 (Pacific Standard Time)"
Alexander O'Mara
  • 58,688
  • 18
  • 163
  • 171
Jason M
  • 1,013
  • 13
  • 25
  • The string returned by [*Date.prototype.toTimeString*](http://www.ecma-international.org/ecma-262/6.0/index.html#sec-date.prototype.totimestring) is **entirely** implementation dependant, it need not contain any timezone information at all. – RobG Nov 12 '15 at 23:35
  • have you checked out momentjs? been around awhile and used by alot. if you don't want to use the library then check out the source. here's a recent SO question that is probably a dup of what you are asking if you go the momentjs route. http://stackoverflow.com/questions/28374901/get-timezone-abbreviation-using-offset-value – hubson bropa Nov 12 '15 at 23:38

3 Answers3

3

Some background:

  • Time zone names and their abbreviations aren't officially standardised, though there are a number of schemes that have been widely adopted and might be considered de facto standards (e.g. IANA time zone database)
  • Browsers are notoriously bad at internationalisation, e.g. IE and Chrome report times for eastern Australia as "E. Australia Standard Time" when it is known much more widely as "Australian Eastern Standard Time"
  • Some time zone schemes do not have unique identifiers for every time zone, e.g. EST might apply to 3 different zones.
  • The value returned by toTimeString is entirely implementation dependent, so it may not contain any time zone information at all and even if it does, it may not be what the user expects (see above)

There are various libraries that determine likely time zones based on the time zone offset returned by Date.prototype.getTimezoneOffset and testing for 2 dates to see if daylight saving is used (e.g. jsTimezoneDetect), however most are based on the IANA time zone database.

So to rephrase your question:

Is there a reliable method to get the host time zone abbreviation

The answer is no.

However, if you're prepared to accept derived IANA time zone designations and map them to whatever other scheme you wish to employ, then using a library will get fairly close.

RobG
  • 142,382
  • 31
  • 172
  • 209
  • Good explanation !! Which are the libraries in AngularJS that can help us to yield the intended result? – Santanu Oct 05 '16 at 21:39
  • You might try the [*angular-moment project*](https://github.com/urish/angular-moment), see the accepted answer here: [*how to get full momentjs api inside angular view?*](http://stackoverflow.com/questions/23727411/how-to-get-full-momentjs-api-inside-angular-view). – RobG Oct 05 '16 at 22:36
2

MomentJS Timezone:

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
Jared Dykstra
  • 3,596
  • 1
  • 13
  • 25
  • Moment.js (and similar libraries) can only guess at the timezone based on the offset and whether or not daylight saving is observed. However, there may be more than one timezone that meets a particular offset/DST combination. – RobG Nov 12 '15 at 23:40
  • @RobG Is this really something specific to libraries? Seems more like a symptom of javascript browser implementation. Most interested if you know a native js cross browser compliant way that doesn't guess :) – hubson bropa Nov 12 '15 at 23:49
  • @hubsonbropa—libraries can only use *timezoneOffset*, then work out whether DST is observed by testing dates in June and January, which tells them northern/southern hemisphere also. But that's it. It's actually reasonably reliable, but not 100% (e.g. if DST isn't observed, they can't tell the hemisphere). – RobG Nov 12 '15 at 23:55
0

Try

Intl.DateTimeFormat().resolvedOptions().timeZone

Waring: It is not compatible with all browser version

Dipu R
  • 545
  • 1
  • 5
  • 22