dayjs icon indicating copy to clipboard operation
dayjs copied to clipboard

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

Open rseiffert opened this issue 1 year ago • 1 comments

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)

rseiffert avatar Nov 01 '24 07:11 rseiffert

We're having the same issue, even for something simple:

  const time1 = dayjs.duration(dayjs('1970-02-28T23:59:59Z').diff(dayjs('1970-01-01T00:00:00Z')))
time1.hours();  // returns 11, should be 23

tomardern avatar May 01 '25 18:05 tomardern