首页 > 解决方案 > 在时刻 js 上使用 endOf('quarter') 后时区发生变化

问题描述

我正在使用以下方法来结束一个季度:

function convertToMomentDates(dates) {
  return dates.map((date) => moment(date).tz("Europe/Rome"));
}

function getEndOfQuarter(dates) {
    const lastDate = dates.pop();
  if (!lastDate || !lastDate.isValid()) return null;
    console.log(lastDate.endOf("quarter").format());
    return lastDate.endOf("quarter").format();
}

const dates = [
  "2018-10-01T01:00:00+01:00",
  "2019-01-01T01:00:00+01:00",
];

const endOfQuarterResult = getEndOfQuarter(convertToMomentDates(dates));

console.log(
  endOfQuarterResult ===
    "2019-03-31T23:59:59+01:00"
);

document.getElementById('result').innerHTML = endOfQuarterResult;
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.18.1/moment.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment-timezone/0.5.14/moment-timezone-with-data.min.js"></script>
<div id="result"></div>

我的意思是我正在使用带有 +1:00 偏移量的所有日期,并且在 endOf 函数之后它被更改为 +2:00,为了不改变偏移量,正确的实现是什么......

标签: javascriptnode.jsmomentjs

解决方案


在这种情况下,由于 3 月底的意大利/罗马时区,预期结果是可以的,在这种情况下,最后一个季度末,偏移量变为 +2。更多信息可以在这个网址上看到:https ://www.timeanddate.com/time/zone/italy/rome 。


推荐阅读