【问题标题】:MomentJS diff is adding an hourMomentJS diff 增加了一个小时
【发布时间】:2020-03-18 15:01:06
【问题描述】:

我正在这样做

const currentCETTime = moment.tz('2020-03-18 15:58:38', 'Europe/Madrid');
const limitCETTime = moment.tz('2020-03-18 18:00:00', 'Europe/Madrid');
console.log('current',currentCETTime.format('HH:mm:ss'));
console.log('limit', limitCETTime.format('HH:mm:ss'));
const seconds = Math.abs(limitCETTime.diff(currentCETTime) / 1000);
console.log('hours', (seconds / 60) / 60);
const rem = moment(seconds * 1000);
console.log('diff', moment(rem).tz('Europe/Madrid').format('HH:mm'));
<script src="https://momentjs.com/downloads/moment.min.js"></script>
<script src="https://momentjs.com/downloads/moment-timezone-with-data-10-year-range.min.js"></script>

我得到了一个错误的区别:

我应该得到 2:01 小时而不是 03:01 的时差

【问题讨论】:

  • 我将您的代码转换为您可以在问题中执行的代码块。我还删除了您对 momentTZ 所做的任何别名,因为它没有定义。如果更改未产生您预期的结果,请根据需要进一步编辑它,但请尽量保留它,以便它成为您可以执行的代码片段。
  • 谢谢伊戈尔,我不知道该怎么做...

标签: javascript momentjs moment-timezone


【解决方案1】:

如果您打印出rem 的整个日期,您会看到问题:

const currentCETTime = moment.tz('2020-03-18 15:58:38', 'Europe/Madrid');
const limitCETTime = moment.tz('2020-03-18 18:00:00', 'Europe/Madrid');
console.log('current',currentCETTime.format('HH:mm:ss'));
console.log('limit', limitCETTime.format('HH:mm:ss'));
const seconds = Math.abs(limitCETTime.diff(currentCETTime) / 1000);
console.log('hours', (seconds / 60) / 60);
const rem = moment(seconds * 1000);
console.log('diff', moment(rem).tz('Europe/Madrid').toString());
<script src="https://momentjs.com/downloads/moment.min.js"></script>
<script src="https://momentjs.com/downloads/moment-timezone-with-data-10-year-range.min.js"></script>

在毫秒值上调用 moment 会产生一个新的纪元日期。

以秒为单位的差异是正确的。如果您想要小时数的差异,您可以使用 hours 参数调用 diff。请注意,矩向下舍入到最接近的整数值。

const currentCETTime = moment.tz('2020-03-18 15:58:38', 'Europe/Madrid');
const limitCETTime = moment.tz('2020-03-18 18:00:00', 'Europe/Madrid');
console.log('current',currentCETTime.format('HH:mm:ss'));
console.log('limit', limitCETTime.format('HH:mm:ss'));
const hours = limitCETTime.diff(currentCETTime, 'hours');
console.log('hours', hours);
<script src="https://momentjs.com/downloads/moment.min.js"></script>
<script src="https://momentjs.com/downloads/moment-timezone-with-data-10-year-range.min.js"></script>

【讨论】:

    【解决方案2】:

    如果您只需要小时和秒,则无需使用时区。您可以调用 utc 然后格式化。检查下面的代码。

    const currentCETTime = moment.tz('2020-03-18 15:58:38', 'Europe/Madrid');
    const limitCETTime = moment.tz('2020-03-18 18:00:00', 'Europe/Madrid');
    console.log('current',currentCETTime.format('HH:mm:ss'));
    console.log('limit', limitCETTime.format('HH:mm:ss'));
    const seconds = Math.abs(limitCETTime.diff(currentCETTime) / 1000);
    console.log('hours', (seconds / 60) / 60);
    const rem = moment(seconds * 1000);
    console.log('diff', moment(rem).utc().format('HH:mm'));
    <script src="https://momentjs.com/downloads/moment.min.js"></script>
    <script src="https://momentjs.com/downloads/moment-timezone-with-data-10-year-range.min.js"></script>

    【讨论】:

      【解决方案3】:

      moment(milliseconds),以及 tz 变体,将毫秒添加到日期 1970-01-01 01:00:00。这是此方法的 unix timestamp parser 行为。这就是为什么在格式化时,它会返回 似乎 的额外小时,但这是因为它从 1 小时而不是 0 小时开始。您可以通过传入 0 来检查此行为的总毫秒数你会看到它返回的小时值是 1。

      【讨论】:

        猜你喜欢
        • 2021-12-29
        • 1970-01-01
        • 2015-09-14
        • 2019-05-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多