Sometimes, we want to display a users local zone abbreviation with JavaScript.
In this article, we’ll look at how to display a users local zone abbreviation with JavaScript.
How to display a users local zone abbreviation with JavaScript?
To display a users local zone abbreviation with JavaScript, we can use moment-timezone.
For instance, we write:
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.1/moment.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment-timezone/0.5.34/moment-timezone.min.js"></script>
to add moment.js and moment-timezone.
Then we write:
moment.tz.add([
'America/Los_Angeles|PST PDT|80 70|0101|1Lzm0 1zb0 Op0',
'America/New_York|EST EDT|50 40|0101|1Lz50 1zb0 Op0'
]);
const tz = moment.tz("America/New_York").zoneAbbr()
console.log(tz)
to call moment.tz.add
to add the time zone data.
Then we call moment.tz
with the time zone string we just added to return the time zone object.
And then we call zoneAbbr
to return the time zone’s abbreviation.
Therefore, tz
is 'EDT'
.
Conclusion
To display a users local zone abbreviation with JavaScript, we can use moment-timezone.