最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

date - Javascript getTimezoneOffset vs moment utcOffset - Stack Overflow

programmeradmin3浏览0评论

I was surprised that new Date().getTimezoneOffset() returns the opposite of moment().utcOffset()

Examples:

  • Timezone UTC +08:00 Hong Kong
    • JS: -480
    • Moment: 480
  • Timezone UTC -06:00 Central America
    • JS: 360
    • Moment: -360
  • Timezone UTC London
    • JS: 0
    • Moment: -0

Question:
If I am correct momentjs is returning the correct value. So why is Javascript new Date().getTimezoneOffset() returning the opposite timezone offset?

I was surprised that new Date().getTimezoneOffset() returns the opposite of moment().utcOffset()

Examples:

  • Timezone UTC +08:00 Hong Kong
    • JS: -480
    • Moment: 480
  • Timezone UTC -06:00 Central America
    • JS: 360
    • Moment: -360
  • Timezone UTC London
    • JS: 0
    • Moment: -0

Question:
If I am correct momentjs is returning the correct value. So why is Javascript new Date().getTimezoneOffset() returning the opposite timezone offset?

Share Improve this question asked Nov 24, 2015 at 10:15 hwcverwehwcverwe 5,3677 gold badges36 silver badges64 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 5

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.

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

The accepted answer explains what getTimezoneOffset does but Moment.js docs explicitly describe the difference between moment utcOffset and the date getTimezoneOffset.

Note: Unlike moment.fn.zone this function returns the real offset from UTC, not the reverse offset (as returned by Date.prototype.getTimezoneOffset).

Source: https://momentjs./docs/#/manipulating/utc-offset/

发布评论

评论列表(0)

  1. 暂无评论