Moment-timezone: Getting timezone from UTC offset instead of timezone "name"

Created on 4 Aug 2016  路  7Comments  路  Source: moment/moment-timezone

Hi there!

I am wondering if it's possible to get the timezone based off of the timezonOffset as opposed to the "name" of the timezone. Something like below:

let date = 'some ISO datetime string';
let timezoneOffset = new Date().getTimeZoneOffset();
let formattedDate = moment(date).tz(timezoneOffset).format('h:mm A z');  // Where z would be EST, for instance

Most helpful comment

What if I'm just interested in this for a momentary check. I understand that, with Daylight Savings or other time-changing laws, a particular time zone does not have a stable offset over time. But if I just need to convert +09:00 to a timezone name for the next few milliseconds while I respond to a single request, and I don't want to update all of my moment().tz(timezone) parsers, I'd love to be able to convert +09:00 into a timezone name (e.g. America/New_York) that has that offset right now.

The naive approach of iterating through them all until I find on that matches would work, but wondering if there's a better way.

Thoughts @joshgarwood @maggiepint ?

All 7 comments

No. This is because multiple timezones share the same offset. See the time zone tag on stack overflow:
http://stackoverflow.com/tags/timezone/info

In particular, check out the TimeZone != Offset section.

You can also see this recording of me speaking at JavaScript MN - it covers this in some detail:
https://www.youtube.com/watch?v=ieIzNP6gKqU

Or, if you really want to go deep on this stuff, there's Matt's pluralsight course:
https://www.pluralsight.com/courses/date-time-fundamentals

You can watch the time zone section, but you should start with the intro to get vocabulary.

Oh wow, thanks so much for the prompt and detailed response! That makes sense then; sorry for the trouble!

What if I'm just interested in this for a momentary check. I understand that, with Daylight Savings or other time-changing laws, a particular time zone does not have a stable offset over time. But if I just need to convert +09:00 to a timezone name for the next few milliseconds while I respond to a single request, and I don't want to update all of my moment().tz(timezone) parsers, I'd love to be able to convert +09:00 into a timezone name (e.g. America/New_York) that has that offset right now.

The naive approach of iterating through them all until I find on that matches would work, but wondering if there's a better way.

Thoughts @joshgarwood @maggiepint ?

@tsheaff I've found a library for this and it seems to work 馃帀
https://pub.dev/packages/flutter_native_timezone

I don't even want to know the offset name, I just want to be able to provide a timestamp like 2019-06-10T10:00:00+12:00 and not have it be converted (parse it and retain the offset so that other functions work). This seems like a common use case (ISO8601 timestamps have offsets, not zone names) but doesn't seem to be supported (unless I'm missing something in the docs).

I don't know how people use moment-timezone, but I just want to convert a time from one offset to another. I don't care what the actual city, country and state are. I have the target offset as it was in the device when the timestamp was stored to the database in the server's time timezone. Daylight saving may or may not be in the offset. I don't know and I don't care, but the offset is exactly how it should be.

I ended up doing this to to get a time, which can be used to check if the device timestamp is in the daytime, evening or at night.

moment.tz(myServerDate.getTime() + minuteOffsetFromTheDevice * 60 * 1000, 'Etc/UTC')

Was this page helpful?
0 / 5 - 0 ratings