Pier-Luc Gendreau Pier-Luc Gendreau - 10 months ago 52
Javascript Question

Guesstimate time zone from time offset with JavaScript and Moment.js

I know this is not going to be foolproof because an offset isn't specific to a timezone, but with location data it seems like it would be possible to make an educated guess.

Basically, I would like to be able to take an object similar to this:

offset: -5,
location: 'America'

...and get back either a single or multiple matching time zones, which would be:

['America/Montreal', 'America/New_York', ...]

One solution I can think of is iterating through the zone data provided by
, but that just doesn't seem like an elegant way to get this info.

Any ideas?


moment-timezone@0.5.0 added moment.tz.guess() which attempts to guess the user's most likely timezone by looking at Date#getTimezoneOffset and Date#toString. It then picks the zone with the largest population. It's not perfect, but it's close enough!



//= America/New_York (I'm in America/Montreal, but this works too)