Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

dayjs duration Incorrect calculation #2726

Open
15678871232 opened this issue Sep 14, 2024 · 2 comments
Open

dayjs duration Incorrect calculation #2726

15678871232 opened this issue Sep 14, 2024 · 2 comments

Comments

@15678871232
Copy link

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 ]
@juliusv
Copy link

juliusv commented Sep 16, 2024

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
Copy link

juliusv commented Sep 16, 2024

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants