Reputation: 1053
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)"
Upvotes: 4
Views: 1963
Reputation: 575
Try
Intl.DateTimeFormat().resolvedOptions().timeZone
Waring: It is not compatible with all browser version
Upvotes: 0
Reputation: 147513
Some background:
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.
Upvotes: 3
Reputation: 3626
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
Upvotes: 2