Skip to content Skip to sidebar Skip to footer

Date.gettimezoneoffset() Is Not Returning An Expected Value

My browser is running in the Eastern Standard Timezone, when call I call date.getTimezoneOffset() I expect -300 to be returned but instead I get 300 var date = new Date(); date.get

Solution 1:

From the Mozilla docs (or devdocs.io):

Return value The time-zone offset is the difference, in minutes, between UTC and local time. Note that this means that the offset is positive if the local timezone is behind UTC and negative if it is ahead. For example, if your time zone is UTC+10 (Australian Eastern Standard Time), -600 will be returned. Daylight saving time prevents this value from being a constant even for a given locale.

EST is behind UTC, therefore you're getting a positive result.

I agree it's not a great way to represent it - I'm much more used to an offset being "the amount of time you add to UTC to get local time" but it is at least behaving as documented...

Solution 2:

It's the difference in minutes from UTC to the timezone you're in. UTC - EST = 300.

From MDN's reference description of the function:

The time-zone offset is the difference, in minutes, between UTC and local time. Note that this means that the offset is positive if the local timezone is behind UTC and negative if it is ahead. For example, if your time zone is UTC+10 (Australian Eastern Standard Time), -600 will be returned. Daylight saving time prevents this value from being a constant even for a given locale.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/getTimezoneOffset

Post a Comment for "Date.gettimezoneoffset() Is Not Returning An Expected Value"