25

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!

2

5 Answers 5

40

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.

Sign up to request clarification or add additional context in comments.

9 Comments

I already had that, but couldn't get it run. Can maybe someone fiddle this for me? Thanks so much!
sure. do have cdn link for momentjs
Looks nice, but can't get it to work. Nothing shows up :(
i have edited the answer with working fiddle. Please refer it and you also need to add two momentjs libraries that i have attached to fiddle from cdn
Oh my god! I really forgot to include moment-timezone-with-data.js. Please don't tell anyone! Sorry, sorry, sorry! Works now. But I don't get the .zoneAbbr(); to show up.
|
1

I'm just doing this. The z in the format adds the proper abbreviation for the timezone.

 moment().tz('America/New_York').format('MMMM Do YYYY, h:mm a z');

Comments

1

.format("z") with a lowercase z prints out just the timezone abbreviation.

Here are a few examples:

let currentTime = moment().tz(moment.tz.guess());
console.log(currentTime.format("z")); // PST

let parisTime = moment().tz("Europe/Paris");
console.log(parisTime.format("z")); // CET

let indiaTime = moment().tz("Asia/Kolkata");
console.log(indiaTime.format("z")); // IST

let newYorkTime = moment().tz("America/New_York");
console.log(newYorkTime.format("z")); // EST

let newYorkTimeDuringDST = moment("2020-08-01").tz("America/New_York");
console.log(newYorkTimeDuringDST.format("z")); // EDT

1 Comment

This is deprecated as of 1.6.0 momentjs.com/docs/#/displaying/format
0
var timeZone = moment.tz.guess();
const zoneName = moment.tz(timeZone).zoneName();
return moment().tz("America/New_York").zoneName();

1 Comment

While this code may provide a solution to problem, it is highly recommended that you provide additional context regarding why and/or how this code answers the question. Code only answers typically become useless in the long-run because future viewers experiencing similar problems cannot understand the reasoning behind the solution.
-1

Include moment.js and moment-timezone-with-data.js javascript library.

<script src="//cdnjs.cloudflare.com/ajax/libs/moment.js/2.22.2/moment.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/moment-timezone/0.5.11/moment-timezone-with-data.js"></script>

Then use the below code to get the browser timezone name.

<script>
var timezone = moment.tz.guess();
console.log(timezone);
</script>

See the timezone name in the browser console.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.