iamkun / iamkun/dayjs

The add/subtract function behaves incorrectly on leap year

Open
#2,698 0 comments 1 reaction 0 assignees View on GitHub
Dominant language
JavaScript
Stars
48.7k
Forks
2.8k
PR merge metrics
No merged PRs in 30d

Description

The add/subtract function behaves incorrectly on leap year.

[dayjs 1.11.12]
console.log(dayjs('2023-01-01').add(dayjs.duration(365, 'day')).toDate()); // Mon Jan 01 2024 00:00:00 GMT+0900
console.log(dayjs('2024-01-01').add(dayjs.duration(365, 'day')).toDate()); // Wed Jan 01 2025 00:00:00 GMT+0900 <-- **wrong**
console.log(dayjs('2025-01-01').add(dayjs.duration(365, 'day')).toDate()); // Thu Jan 01 2026 00:00:00 GMT+0900

console.log(dayjs('2024-01-01').subtract(dayjs.duration(365, 'day')).toDate()); // Sun Jan 01 2023 00:00:00 GMT+0900
console.log(dayjs('2025-01-01').subtract(dayjs.duration(365, 'day')).toDate()); // Mon Jan 01 2024 00:00:00 GMT+0900 <-- **wrong**
console.log(dayjs('2026-01-01').subtract(dayjs.duration(365, 'day')).toDate()); // Wed Jan 01 2025 00:00:00 GMT+0900

[moment.js]
console.log(moment('2023-01-01').add(moment.duration(365, 'day')).toDate()); // Mon Jan 01 2024 00:00:00 GMT+0900
console.log(moment('2024-01-01').add(moment.duration(365, 'day')).toDate()); // Tue Dec 31 2024 00:00:00 GMT+0900 <-- **correct**
console.log(moment('2025-01-01').add(moment.duration(365, 'day')).toDate()); // Wed Jan 01 2025 00:00:00 GMT+0900

console.log(moment('2024-01-01').subtract(moment.duration(365, 'day')).toDate()); // Sun Jan 01 2023 00:00:00 GMT+0900
console.log(moment('2025-01-01').subtract(moment.duration(365, 'day')).toDate()); // Tue Jan 02 2024 00:00:00 GMT+0900 <-- **correct**
console.log(moment('2026-01-01').subtract(moment.duration(365, 'day')).toDate()); // Wed Jan 01 2025 00:00:00 GMT+0900

**Information**
- Day.js Version: v1.11.12
- OS: Windows 10
- Browser: chrome 126
- Time zone: GMT+0900 (日本標準時)

Contributor guide

Open the contributing guide

Research direction

Start by reproducing the add and subtract examples using dayjs.duration(365, 'day'), focusing on the leap-year cases shown in the issue. Trace the add/subtract and duration entry points, then verify that the results match the expected dates for both directions and the non-leap-year examples.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript
Domain
api
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.