I need library that can convert time zone to utc offset. I have list of Americans cities, and I know their zones (PST, EST ....) and locations. So my strategy can be
- library.getOffset('city location')
- myLibraryOrOtherLibrary.getOffset('EST')
I know about library moment.js .But library doesn't support all Americans cities for something like below
moment().tz("America/Los_Angeles").format();
Should I implement something myself or there is some other library or I can use moment.js in some other way?
A few things:
You cannot get a time zone or offset from an abbreviation alone, because there are ambiguities like CST
which could be -5, -6, or +8 depending on how you interpret it ("Central Standard Time", "Cuba Standard Time", "China Standard Time").
You also cannot get a time zone or offset from a city name alone, because there are many cities with the same name in different locations across the world. For example, Paris
could mean Paris, France or it could mean Paris, Texas, USA, or any of the other places called Paris. You also might get different spellings of a city for different languages, depending on the city, such as the capital of Ukraine being spelled Kiev
in English and Kyiv
in Ukrainian.
Moment supports the standard list of IANA time zone identifiers. They aren't all the cities of the world, but rather a specific string that sometimes is based on a major city name. There are plenty of exceptions.
Ultimately, the best way to solve this problem is in two steps:
Resolve your locations to approximate latitude and longitude coordinates. This is called geocoding, and there are a number of APIs and databases available that can help you with this. For example, Google Maps Geocoding API.
Resolve the coordinates to an IANA time zone identifier. There are many methods for this, including the Google Maps Time Zone API, and many others listed in this answer. You can then use this identifier with Moment.js and others.