Skip to content

DateTime.diff produces wrong results with unit quarter #1266

@Cameramorphic

Description

@Cameramorphic

Describe the bug
When diffing two dates using a unit combined with 'milliseconds', where the duration between the dates is a multiple of this unit, the resulting duration has unexpected values if the unit is 'quarter'.
As far as I can tell, this happens if the year of the two dates is different.
It works as expected for the units 'month' and 'year'.

The 'milliseconds' of the duration act as the remainder, if it is 0 the duration is a multiple of unit.

To Reproduce

const d = DateTime.fromISO('2023-01-01').diff(DateTime.fromISO('2022-01-01'), ['quarter', 'milliseconds']);
const d2 = DateTime.fromISO('2023-05-01').diff(DateTime.fromISO('2022-02-01'), ['quarter', 'milliseconds']);

Actual vs Expected behavior
Actual: d is { quarters: 0, milliseconds: 31536000000 } (a year in ms).
Expected: d should be { quarters: 4, milliseconds: 0 }.

Actual: d2 is { quarters: 1, milliseconds: 31536000000 } (a year in ms).
Expected: d2 should be { quarters: 5, milliseconds: 0 }.

My current workaround:
I check if the unit is 'quarter' and use 'month' instead.
Afterwards I additionally have to check if duration.quarters % 3 === 0.

Desktop:

  • OS: Ubuntu 20.04 LTS
  • Browser: Chrome 104
  • Luxon version: 3.0.1
  • Your timezone "Europe/Berlin"

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions