3

I am using moment timezone for timezone calculations. All I have some offset data coming from the database like GMT, GMT+1, GMT+2, GMT+3, GMT+4 etc.

Is their anyway i could get the timezone or timezone name like "America/Los_Angeles" from this data?

informatik01
  • 16,038
  • 10
  • 74
  • 104
Vinz and Tonz
  • 3,487
  • 5
  • 21
  • 32

3 Answers3

6

I don't mean to be rude, but seriously - How many times and how loudly do I have to shout this?

Time Zone != Offset

I mean - it's only in the tag header, and the body of the timezone tag wiki:

image

...

image

And it's not like I haven't already answered this question, or this one, or addded it to the best practices doc, or littered what must be hundreds of other posts with "Time Zone != Offset" comments...

I know, maybe I'll go talk about this at a high profile developer conference with "Time Zone != Offset" on my shirt just so it sinks in. Oh wait - done that already too.

image

Apologies in advance for the snarky answer, but close as duplicate of duplicate of duplicate just wasn't as much fun.

Community
  • 1
  • 1
Matt Johnson-Pint
  • 230,703
  • 74
  • 448
  • 575
  • Thanks @Matt Johnson .. sorry for the inconvenience caused .I am not good with timezones .. and i am really stuck with a production app . – Vinz and Tonz Apr 22 '16 at 01:34
  • 1
    No problem, not an inconvenience at all. And I apologize for being condescending in the answer. I really do like to help on these issues, so if you still have questions, please do ask. It's just this particular one comes up again and again, and the fact is that a time zone offset and a time zone are quite different. **However**, if you are only working with fixed time zone offsets, keep in mind that moment.js does have the `utcOffset` function which supports those. They just don't have any of the properties of real-world time zones, like daylight saving time and other historical changes. – Matt Johnson-Pint Apr 22 '16 at 03:16
0

Have you tried http://momentjs.com/timezone/ already ?

Not 100% sure but I think this is what you are looking for.

Nikhil Maheshwari
  • 2,198
  • 18
  • 25
0

Matt's answer is correct but in some cases, one might still need the closest (not the most accurate) timezone name from the offset. You can use moment-timezone package. Here's some Typescript code to get a map from offset to time zone name.

const timeZoneCache = new Map<string, string>();
const allTimeZoneNames = moment.tz.names();
const currDate = moment();
for (const timeZoneName of allTimeZoneNames) {
    timeZoneCache.set(currDate.tz(timeZoneName).format("Z"), timeZoneName);
}
jarora
  • 5,384
  • 2
  • 34
  • 46