I am using moment.js and would like to show the user's local timezone name like CET or PST using
var timezone_local = moment.tz().zoneName(); document.getElementById('timezone_local').innerHTML = timezone_local;
Those lines do not work. Thanks for your help!
var tz = moment. tz. guess(); It will return an IANA time zone identifier, such as America/Los_Angeles for the US Pacific time zone.
The JavaScript getTimezoneOffset() method is used to find the timezone offset. It returns the timezone difference in minutes, between the UTC and the current local time. If the returned value is positive, local timezone is behind the UTC and if it is negative, the local timezone if ahead of UTC.
By default, moment objects are created in the local time zone. Local time zone - it's a time zone which is set in a browser or on your node. js server. To change the default time zone, use moment.
For example, if you parse the string '2020/01/02' and then call the moment#tz() method, you're telling moment to parse the string in the locale time, and then convert the date to a given IANA timezone. // '20200101 21:00 PST' moment('2020/01/02', 'YYYY/MM/DD'). tz('America/Los_Angeles'). format('YYYYMMDD HH:mm z');
According to the official moment document, you can use moment-timezone
moment.tz.guess();
For further formatting, refer to this.
Edited :
var zone_name = moment.tz.guess(); var timezone = moment.tz(zone_name).zoneAbbr() console.log(timezone);
Refer to this working fiddle.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With