如何获得时区的标准偏移量

How can I get standard offset of a timezone with momemnt

我需要找到特定时区的偏移量。如何使用 moment 来做到这一点?

假设我的客户端时区是 MST,我想找到 EST 时区偏移量。我需要在不考虑夏令时的情况下获得标准偏移量。

使用 moment.tz("America/Edmonton").format('Z') 我得到 -6:00 但这考虑了夏令时。我想要一些东西给我 -7:00 因为它是标准偏移量。

这样的怎么样?

function getStandardOffset(zone) {

  // start with now
  var m = moment.tz(zone);

  // advance until it is not DST
  while (m.isDST()) {
    m.add(1, 'month');
  }

  // return the formatted offset
  return m.format('Z');
}

getStandardOffset('America/Edmonton')  // returns "-07:00"

当然,这个returns当前标准偏移量。如果时区过去使用了不同的标准偏移量,则您需要从该范围内的时刻开始,而不是 "now".

在永久 DST 时区的情况下,它将是一个无限循环,对吗? 所以我的代码最终是:

function getStandardOffset(zone) {

  // start with now
  var m = moment.tz(zone);

  // advance until it is not DST
  var counter = 1;
  while (m.isDST()) {
        m.add(1, 'month');
        if (counter > 12)
        {
            break;
        }
  }

  // return the formatted offset
  return m.format('Z');
}