Detect timezone abbreviation using JavaScript
The Date
object doesn't have a method for getting the timezone abbreviation, but it is implicit at the end of the result of toString
. For example,
var rightNow = new Date();
alert(rightNow);
...will return something like Wed Mar 30 2011 17:29:16 GMT-0300 (ART)
. The timezone abbreviation can be isolated between parentheses:
var rightNow = new Date();
alert(String(String(rightNow).split("(")[1]).split(")")[0]);
The output will be the timezone abbreviation, like ART
.
A native solution:
var zone = new Date().toLocaleTimeString('en-us',{timeZoneName:'short'}).split(' ')[2]
console.log(zone)
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toLocaleDateString
You can pass undefined
instead of en-us
to default to the browser's current locale.
moment-timezone includes an undocumented method .zoneAbbr()
which returns the time zone abbreviation. This also requires a set of rules which are available to select and download as needed.
Doing this:
<script src="moment.js"></script>
<script src="moment-timezone.js"></script>
<script src="moment-timezone-data.js"></script>
<script>
moment().tz("America/Los_Angeles").zoneAbbr();
</script>
Returns:
'PDT' // As of this posting.
Edit (Feb 2018)
Evan Czaplicki has worked on a draft proposal to add a time zone API to browsers.