Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Display local timezone name with moment.js

Tags:

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!

like image 696
RobinAlexander Avatar asked Jan 08 '17 18:01

RobinAlexander


People also ask

How do I get local time zones with my moment?

var tz = moment. tz. guess(); It will return an IANA time zone identifier, such as America/Los_Angeles for the US Pacific time zone.

How can I get the timezone name in JavaScript?

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.

Does moment js use local timezone?

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.

How do you get time zone abbreviation in 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');


1 Answers

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.

like image 70
user3775217 Avatar answered Oct 10 '22 04:10

user3775217