简体   繁体   English

Moment.js - 在同一小时内添加分钟到最后一分钟的结果

[英]Moment.js - Adding minute to last minute of hour results in same hour

I'm currently trying to use moment.js (2.24.0) to add a minute to the last minute of a hour.我目前正在尝试使用 moment.js (2.24.0) 在一小时的最后一分钟添加一分钟。 However, it results in going back to the first minute of the same hour.但是,它会导致回到同一小时的第一分钟。

Here's an example code:这是一个示例代码:

  var minute = '2019-11-03 01:59:00';
  console.log('Before: '+minute);
  minute = moment(minute, 'YYYY-MM-DD HH:mm:ss')
    .add(1, 'minutes')
    .format('YYYY-MM-DD HH:mm:00');
  console.log('After: '+minute);

This results in:这导致:

Before: 2019-11-03 01:59:00
After: 2019-11-03 01:00:00

Is this a bug or expected behavior for moment.js?这是 moment.js 的错误还是预期行为? If it's expected behavior, then how do I properly address it to go to the next hour.如果这是预期的行为,那么我如何在下一小时内正确地将其发送到 go。

In the US, daylight savings time ends at 2AM on 2019-11-03.在美国,夏令时于 2019 年 11 月 3 日凌晨 2 点结束。

The behaviour you're seeing when adding a minute to 1:59AM isn't a bug but rather what will actually happen at that time.您在凌晨 1:59 添加一分钟时看到的行为不是错误,而是当时实际发生的情况。 The calculation is based on your current timezone so 2019-11-03 01:59:00 EDT (Eastern Daylight Time) + 1 minute = 2019-11-03 01:00:00 EST (Eastern Standard Time).该计算基于您当前的时区,因此 2019-11-03 01:59:00 EDT(东部夏令时间)+ 1 分钟 = 2019-11-03 01:00:00 EST(东部标准时间)。

If you were to run the same test at 2:59 instead of 1:59, it would increment as expected.如果您要在 2:59 而不是 1:59 运行相同的测试,它将按预期增加。 You could specify that you actually mean 1:59AM EST but be sure that is in fact the case before attempting it.您可以指定您实际上是指美国东部标准时间上午 1:59,但在尝试之前请确保确实如此。

Additional reading: https://momentjs.com/timezone/docs/#/using-timezones/parsing-ambiguous-inputs/补充阅读: https://momentjs.com/timezone/docs/#/using-timezones/parsing-ambiguous-inputs/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM