You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

moment.js处理2021年10月31日时间加24小时异常问题求助

Troubleshooting Moment.js 24-Hour Addition Issue on 2021-10-31

Hey there! Let's break down why you're seeing this unexpected behavior with Moment.js when adding 24 hours to October 31, 2021—it all comes down to Daylight Saving Time (DST) transitions.

The Root Cause

Many regions (like most of Europe) end Daylight Saving Time on the last Sunday of October. In 2021, that date was October 31st. On this day, clocks are rolled back by 1 hour at 2 AM (local time), meaning the day effectively has 25 hours instead of the usual 24.

When you use Moment.js's default local timezone parsing, it accounts for this DST shift. So when you add 24 hours to 2021-10-31 00:00 local time:

  • The actual elapsed time is 24 hours, but due to the clock rollback, the resulting local time lands at 2021-10-31 23:00 instead of 2021-11-01 00:00 as you expected.

For 2020 and 2022, October 31st wasn't the DST transition date:

  • 2020's DST ended on October 25th (last Sunday of the month)
  • 2022's DST ended on October 30th
    So those dates had standard 24-hour days, hence adding 24 hours worked as expected.

Fixes to Get the Expected Result

You have two reliable ways to avoid this issue:

1. Use UTC Time (Recommended if Timezone Isn't Critical)

Moment's utc() method parses and manipulates dates in UTC, which doesn't observe DST. This ensures consistent 24-hour increments regardless of local timezone rules:

console.log("October 2021 (UTC)")
var a = moment.utc("2021103100", "YYYYMMDDHH").add(23, 'hours').format('YYYYMMDDHH');
console.log(a); // Output: 2021103123
var a = moment.utc("2021103100", "YYYYMMDDHH").add(24, 'hours').format('YYYYMMDDHH');
console.log(a); // Output: 2021110100
var a = moment.utc("2021103100", "YYYYMMDDHH").add(25, 'hours').format('YYYYMMDDHH');
console.log(a); // Output: 2021110101

console.log("October 2020 (UTC)")
var a = moment.utc("2020103100", "YYYYMMDDHH").add(23, 'hours').format('YYYYMMDDHH');
console.log(a); // Output: 2020103123
var a = moment.utc("2020103100", "YYYYMMDDHH").add(24, 'hours').format('YYYYMMDDHH');
console.log(a); // Output: 2020110100
var a = moment.utc("2020103100", "YYYYMMDDHH").add(25, 'hours').format('YYYYMMDDHH');
console.log(a); // Output: 2020110101

console.log("October 2022 (UTC)")
var a = moment.utc("2022103100", "YYYYMMDDHH").add(23, 'hours').format('YYYYMMDDHH');
console.log(a); // Output: 2022103123
var a = moment.utc("2022103100", "YYYYMMDDHH").add(24, 'hours').format('YYYYMMDDHH');
console.log(a); // Output: 2022110100
var a = moment.utc("2022103100", "YYYYMMDDHH").add(25, 'hours').format('YYYYMMDDHH');
console.log(a); // Output: 2022110101

2. Explicitly Specify a Timezone (Using Moment-Timezone)

If you need to work with a specific timezone that either doesn't observe DST, or you want to enforce consistent behavior, use moment-timezone to lock the timezone:

// Ensure you've loaded moment-timezone first
console.log("October 2021 (Fixed Timezone)")
var a = moment.tz("2021103100", "YYYYMMDDHH", "UTC").add(24, 'hours').format('YYYYMMDDHH');
console.log(a); // Output: 2021110100

Key Takeaway

When dealing with date arithmetic, always be mindful of timezone rules like DST transitions. Using UTC or explicit timezones eliminates unexpected shifts caused by local timezone adjustments.

内容的提问来源于stack exchange,提问作者Stefano Ferri

火山引擎 最新活动