iamkun / iamkun/dayjs

Inconsistent behaviour of diff(.., 'months) on leap days

Open
#2,595 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
JavaScript
Stars
48.7k
Forks
2.5k
PR merge metrics
No merged PRs in 30d

Description

Bug description

The difference in months between two days yields different results depending on the time of the day on leap days.

```
dayjs = require('dayjs')
customParseFormat = require('dayjs/plugin/customParseFormat')
dayjs.extend(customParseFormat)

dayjs('2024-05-29T21:00:00.000Z').diff(dayjs('2024-02-29T00:00:00.000Z'), 'months') // Result = 3
dayjs('2024-05-29T22:00:00.000Z').diff(dayjs('2024-02-29T00:00:00.000Z'), 'months') // Result = 2 <-- this one seems wrong
dayjs('2024-05-29T23:00:00.000Z').diff(dayjs('2024-02-29T00:00:00.000Z'), 'months') // Result = 3
```

Expected behaviour:

The same test on a non leap day:

```
dayjs('2024-05-28T21:00:00.000Z').diff(dayjs('2024-02-28T00:00:00.000Z'), 'months') // Result = 3
dayjs('2024-05-28T22:00:00.000Z').diff(dayjs('2024-02-28T00:00:00.000Z'), 'months') // Result = 3
dayjs('2024-05-28T23:00:00.000Z').diff(dayjs('2024-02-28T00:00:00.000Z'), 'months') // Result = 3
```

**Information**
- Day.js Version 1.11.10
- OS: Windows 10 Enterprise (same result within Ubuntu docker image)
- Browser Nodejs Repl v21.1.0 and Chrome 121.0.6167.18
- Time zone: GMT+01:00

Contributor guide

Open the contributing guide

Research direction

Start by running the reported Node.js reproduction with Day.js 1.11.10, then locate the implementation of diff(..., 'months') and its existing tests. Compare the leap-day and non-leap-day cases across the three times shown. Done means the leap-day examples behave consistently with the expected three-month result and the regression is covered by a test.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.