Duration between dates seems to be incorrect
See original GitHub issueDescribe 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?
Issue Analytics
- State:
- Created 2 years ago
- Reactions:4
- Comments:7
Top Results From Across the Web
How to correct a #VALUE! error in the DAYS function
Solution: Check your system's date and time settings to make sure that they match the format of the dates referred to in the...
Read more >Getting wrong time after calculating difference between two ...
I want to calculate the difference between a certain date and the current time. ... I want to get the amount of days,...
Read more >Zap dates or times are incorrect - Zapier Help
Sometimes you might just want to adjust your date or time by a consistent interval, for instance always seven days ahead of the...
Read more >How do I calculate the interval between two dates?
FAQ: Calculate Interval between Dates (v1.0, 11/15/20) ... can use the datediff function in a calculated field to compute the interval (days, weeks,...
Read more >Excel DATEDIF function to get difference between two dates
To find out how many weeks there are between two dates, you can use the DATEDIF function with "D" unit to return the...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

I just noticed a bug and it seems related to this issue:
I would expect the output to be
{ years: 3, days: 4 }.Further evidence for the above observed in comment on a related issue.
Brief thread on more accurate duration requirements also adds to this.