Duration between dates seems to be incorrect
- Dominant language
- JavaScript
- Stars
- 48.7k
- Forks
- 2.5k
- PR merge metrics
- No merged PRs in 30d
Description
**Describe the bug**
Duration seems to be incorrect. The difference between March 29th, 2020 and March 28th 2021 should be less than 12 months
**Expected behavior**
Calculating the difference between two days one year apart from each other should yield a difference of one year.
**Information**
- Day.js Version: 1.10.3
- OS: Windows 10 20H2
- Browser Chrome 89
- Time zone: GMT -4:00
I'm trying to get the difference between two dates:
- A fixed date (March 29th, 2020)
- Todays date (at the time of writing March 28th, 2021)
I do that with:
```
const lockdown = dayjs("3/29/2020", "M/DD/YYYYY"); //29th March 2020
const currentDate = dayjs();
// get the difference between the moments
const diff = currentDate.diff(lockdown);
//express as a duration
const diffDuration = dayjs.duration(diff);
// display
const timeInLockDown = {
months: diffDuration.months(),
days: diffDuration.days(),
hours: diffDuration.hours(),
minutes: diffDuration.minutes(),
seconds: diffDuration.seconds()
};
```
This yields `diffDuration ` as:
```
days: 4
hours: 1
milliseconds: 709
minutes: 4
months: 12
seconds: 5
years: 0
```
Which doesn't make sense to me? It shouldn't 12 months and 4 days. Also, I think that if it is 12 months and 4 days, then years should be 1 but in this instance it's 0.
Is this expected behaviour?
Contributor guide
Research direction
Start by reproducing the March 29, 2020 to March 28, 2021 example with the shown dayjs.diff and dayjs.duration calls, then inspect how the duration components are derived. Done means the behavior is clarified or corrected for this case, with regression coverage for the reported values and the expected one-year interpretation.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- javascript
- Domain
- tooling
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100