Get utc offset from timezone in javascript

I need a Javascript function that sets the time zone, returns the current utc offset.

For example, theFuncIneed ('US / Eastern') β†’ 240

Any idea?

thanks

+5
javascript timezone datejs
source share
2 answers

In general, this is not possible.

  • US/Eastern is the identifier for the time zone. (This is actually an alias of America/New_York , which is the real identifier.)

  • 240 is the time zone offset. This is most often written as -04:00 (invert the sign, divide by 60).

  • The US eastern time zone includes both Eastern Standard Time, with an offset of -05:00 , and Eastern Daylight Time, which has an offset of -04:00 .

So it’s not at all so to say US/Eastern = 240. Read the time zone wiki , especially the "Time Zone! = Offset" section.

Now you have requested the current offset, which is possible. If you provide date + time, you can solve this problem.

  • In the local time zone of the computer running the javascript code, it is embedded with .getTimeZoneOffset() from any instance of the Date object.

  • But if you want it for a specific time zone, you will need to use one of the libraries I have listed .

+4
source share

You can do this using moment.js

moment.tz ('time zone name'). utcOffset ()

+1
source share

All Articles