dayjs icon indicating copy to clipboard operation
dayjs copied to clipboard

dayjs duration Incorrect calculation

Open 15678871232 opened this issue 1 year ago • 4 comments

Describe the bug A clear and concise description of what the bug is. const duration2023 = this.$dayjs.duration(this.$dayjs('2023-12-31 23:59:59').diff(this.$dayjs('2023-01-01 00:00:00'))); out: { "years": 0, "months": 12, "days": 4, "hours": 23, "minutes": 59, "seconds": 59, "milliseconds": 0 }

const duration2024 = this.$dayjs.duration(this.$dayjs('2024-12-31 23:59:59').diff(this.$dayjs('2024-01-01 00:00:00'))); out: { "years": 1, "months": 0, "days": 0, "hours": 23, "minutes": 59, "seconds": 59, "milliseconds": 0 }

years 0&&1 Expected behavior A clear and concise description of what you expected to happen. Consistent year Information

  • Day.js Version [v1.11.13]
  • OS: [WIN 11]
  • Browser [edge 128.0.2739.67 ]
  • Time zone: [GMT-08:00 ]

15678871232 avatar Sep 14 '24 03:09 15678871232

I think I'm seeing the same similar bug for converting a duration to either hours or days for anything larger than 30 days (it works until that point, breaks down after).

Here's a few simple test cases to reproduce where it breaks:

  // SUCCEEDS, it returns 29 days.
  it('convert just less than 30-something days to days', () => {
    expect(dayjs.duration((86400000 * 30) - 1).days()).toBe(29)
  })
  // FAILS, it returns 0 instead of 30 days.
  it('convert just less than 31-something days to days', () => {
    expect(dayjs.duration((86400000 * 31) - 1).days()).toBe(30)
  })
  // SUCCEEDS, it returns 23 hours.
  it('convert just less than 30-something days to hours', () => {
    expect(dayjs.duration((86400000 * 30) - 1).hours()).toBe(23)
  })
  // FAILS, it returns 13 instead of 23 hours.
  it('convert just less than 31-something days to hours', () => {
    expect(dayjs.duration((86400000 * 31) - 1).hours()).toBe(23)
  })

juliusv avatar Sep 16 '24 09:09 juliusv

Sorry, I wasn't dayjs-ing correctly. 30 is of course the number of days in a month, and months are separately extracted, and then 0 for the days makes sense. And then 13 for the hours comes from the fact that a month is not defined as exactly 30 days, but as const MILLISECONDS_A_MONTH = MILLISECONDS_A_YEAR / 12.

So please ignore my comment.

juliusv avatar Sep 16 '24 09:09 juliusv

We see this too. v1.11.8 was fine, but v1.11.10 and the current version v1.11.13 still exhibit the error. Here's a simple use case performed right from the console on the dayjs website:

dayjs.duration(dayjs('2021-12-31T08:00:00.000').diff(dayjs('2021-01-01T08:00:00.000')))

output:

{
  days: 29,
  hours: 10,
  milliseconds: 0,
  minutes: 0,
  months: 11,
  seconds: 0,
  years: 0
}

mikehallgenesys avatar Nov 25 '24 22:11 mikehallgenesys

Seeing this same issue. If over 30 days and ~8 hours the duration gets reset back to zero and starts over. I need to handle for durations of up to 60 days.

onorok avatar May 07 '25 12:05 onorok

same issue for me

shiwenyu avatar Jul 30 '25 11:07 shiwenyu