user555
user555

Reputation: 1529

moment get time in different timezones

I only have four different timezones

<select class="pull-left marg-l-5px" id="hoursTimezone">
 <option>-</option>
 <option>EST</option>
 <option>CST</option>
 <option>PDT</option>
 <option>MDT</option>
</select>

I added moment timzone file with data to my project, file name is

/moment-timezone-with-data-2010-2020.js

I'M IN IST and typing following command

moment().tz('EST');

Is returning time In IST,if I try the same for other timezones CST,PDT,MDT I'm getting errors

Moment Timezone has no data for ---.

check the following image

enter image description here

what is wrong here?

Is there any way to get time in only these timezones?

or somebody please tell me what is the string for EST and other timezones in moment data string

there are so many string if I search in this file, like

I want something like "query for 'America/Los_Angeles' and you'll get EST time".

Thanks in advance

Upvotes: 0

Views: 906

Answers (1)

Matt Johnson-Pint
Matt Johnson-Pint

Reputation: 241920

You should never rely on time zone abbreviations for selection or input. They are just too ambiguous. "CST" alone has 5 different meanings. See the list on Wikipedia.

Additionally, an abbreviation doesn't necessarily represent the entire time zone. Consider that CST only applies to the North American Central time zone during standard time, while CDT applies during daylight time.

Recognize also that time zones used in moment-timezone must be valid within the IANA time zone database. You can find a list here.

I believe what you are looking for are the time zones applicable in the United States. Assuming you don't care about historical dates, the minimal set would need to also include Arizona, Hawaii, and Alaska. The list would be:

<select class="pull-left marg-l-5px" id="hoursTimezone">
 <option>-</option>
 <option value="America/New_York">Eastern</option>
 <option value="America/Chicago">Central</option>
 <option value="America/Denver">Mountain</option>
 <option value="America/Phoenix">Arizona</option>
 <option value="America/Los_Angeles">Pacific</option>
 <option value="America/Anchorage">Alaska</option>
 <option value="Pacific/Honolulu">Hawaii</option>
</select>

Upvotes: 3

Related Questions