Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get timezone list from moment.js

I need to create a select with the following values,

Eastern Standard Time (EST, GMT-5:00)
Central Standard Time (CST, GMT-6:00)
Mountain Standard Time (MST,GMT-7:00)
Phoenix Standard Time (PNT, GMT-7:00)
Pacific Standard Time (PST, GMT-8:00)

is there a way to do it through moment.js ?

like image 431
Petran Avatar asked Mar 16 '16 19:03

Petran


People also ask

How do you display timezone in moments?

var moment = require('moment-timezone'); moment().tz("America/Los_Angeles").format(); Note: By default, webpack bundles all moment-timezone data (in moment-timezone 0.5.25, that's over 900 KBs minified).

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. Beside, you can use moment. format('zz') to get short timezone name.

What is moment TZ ()?

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').

What is moment-timezone with data?

Moment-Timezone is an add-on for Moment. js. Both are considered legacy projects, now in maintenance mode. In most cases, you should choose a different library. For more details and recommendations, please see Project Status in the Moment docs.


1 Answers

moment.js has timezone support via moment-timezone, so if you load the full set of timezones, you can filter for the time zones you care about by iterating through moment.tz.names(), and then calling format("z, Z") to get the string you want.

> moment.tz("America/Los_Angeles").format("z, Z")
"PDT, -07:00"
> moment.tz("2016-01-01", "America/Los_Angeles").format("z, Z")
"PST, -08:00"
like image 150
user2926055 Avatar answered Sep 30 '22 18:09

user2926055