Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the list of all available timezone using moment-timezone

Tags:

I am trying to get the list of all the available timezones using moment-timezone in node js like this -

var moment = require('moment-timezone'); var timeZones = moment.tz.names(); console.log(timeZones); 

I am getting the timezones in this format -

'Europe/Mariehamn', 'Europe/Minsk', 'Europe/Monaco', 'Europe/Moscow', 'Europe/Nicosia', 'Europe/Oslo', 'Europe/Paris', 'Europe/Podgorica', 'Europe/Prague', 'Europe/Riga', 'Europe/Rome', 

But I want to get the timezones in this format -

(GMT +01:00) Africa/Brazzaville (GMT +01:00) Africa/Casablanca (GMT +01:00) Africa/Douala (GMT +01:00) Africa/El_Aaiun (GMT +01:00) Africa/Kinshasa (GMT +01:00) Africa/Lagos (GMT +01:00) Africa/Libreville (GMT +01:00) Africa/Luanda (GMT +01:00) Africa/Malabo (GMT +01:00) Africa/Ndjamena (GMT +01:00) Africa/Niamey 

How do I get ?

like image 385
Tanmoy Avatar asked Jun 23 '16 12:06

Tanmoy


People also ask

How do you show timezone in moment?

var result = moment(someDate). format("MM/DD/YYYY HH:mm A Z");

How do I get moments from date and time zone?

You can use moment.tz() to get timezone full name. It will return undefined if timezone is not set.

How do I use moment-timezone in typescript?

Use Typescript @types packages and import it via import * as moment from 'moment-timezone'; You can use all moment methods and member vars as moment-timezone exports them. Show activity on this post. Show activity on this post. Show activity on this post.

Does moment use UTC by default?

utc(Date); By default, moment parses and displays in local time. If you want to parse or display a moment in UTC, you can use moment.


1 Answers

There is no straight way of getting in the format you want, directly from moment-timezone.

Try like below.

var moment = require('moment-timezone'); var timeZones = moment.tz.names(); var offsetTmz=[];  for(var i in timeZones) {     offsetTmz.push(" (GMT"+moment.tz(timeZones[i]).format('Z')+") " + timeZones[i]); } 

Now, offsetTmz is an array of strings in the format you want.

This is how I am using it.

Hope this will help you.

like image 90
Shrabanee Avatar answered Sep 22 '22 09:09

Shrabanee