iamkun / iamkun/dayjs

diff and duration calculates wrong days & hours (from v1.11.10 to v1.11.13)

Open
#2,762 1 comment 6 reactions 0 assignees View on GitHub
Dominant language
JavaScript
Stars
48.7k
Forks
2.8k
PR merge metrics
No merged PRs in 30d

Description

**Describe the bug**
I have the following code fragment in a node.js based backend written in Typescript to calculate the difference between two DateTimes:
```
import dayjs from 'dayjs'
import duration from 'dayjs/plugin/duration'
import utc from 'dayjs/plugin/utc'
import timezone from 'dayjs/plugin/timezone'
...
dayjs.extend(duration)
dayjs.extend(utc)
dayjs.extend(timezone)
...
const date1 = dayjs().utc().local().tz('Europe/Berlin');
const date2 = dayjs('2025-03-13T00:00:00').utc().local().tz('Europe/Berlin')
const diff = dayjs.duration(date2.diff(date1))
console.info('date1', date1.format());
console.info('date2', date2.format());
console.info('diff:\n', diff);
```
This returns the wrong number of hours (console-output):
```
date1 2024-11-01T08:27:46+01:00
date2 2025-03-13T00:00:00+01:00
diff:
l {
'$d': {
years: 0,
months: 4,
days: 9, // <== This is wrong
hours: 23, // <= This is wrong
minutes: 32,
seconds: 13,
milliseconds: 588
},
'$l': 'en',
'$ms': 11374333588
}
```
This results in
* 4 months, **9 days, 23 hours**, 32 minutes
(wrong numbers in bold)

**Expected behavior**
The correct result would be
* 4 months, 11 days, 15 hours, 32 minutes

**Additional information**
Up to dayjs v.1.11.9 it works as expected. From v.1.11.10 to 1.11.13 it does not calculate correctly.

**Information**
- Day.js Version v1.11.10 - v1.11.13
- OS: macOS 15.1
- Browser n/a (node.js v23.0.0)
- Time zone: GMT+1 (CET)

Contributor guide

Open the contributing guide

Research direction

Start with the duration, utc, and timezone plugin entry points and reproduce the supplied date range in Node.js across Day.js v1.11.9 through v1.11.13. Compare the duration fields and verify the expected days and hours, then add or update regression coverage for the reported version change.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, node.js, typescript
Domain
backend
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.