duration-fns icon indicating copy to clipboard operation
duration-fns copied to clipboard

Strange result for `.between` for Feb-March

Open forabi opened this issue 1 year ago • 2 comments

d.between(new Date("2024-02-27"), new Date("2024-03-02"))
{
  years: 0,
  months: 1,
  weeks: 0,
  days: -25,
  hours: 0,
  minutes: 0,
  seconds: 0,
  milliseconds: 0
}

forabi avatar Feb 29 '24 00:02 forabi

Actually it seems to happen for any months I tried, wouldn't it make sense for the result to normalized in days?

forabi avatar Feb 29 '24 00:02 forabi

Hi!

You're right, it's a normalization thing.

This is covered in the docs as expected behaviour. Units like "month" are ambiguous. It's impossible to know if you wanted to count it as months or days without a bit more information, so it defaults to what is safe.

To achieve what you want, you can pass the result through .normalize(), using the 2nd argument to specify the original reference date. Bit verbose, but that's how it works at the moment.

dlevs avatar Feb 29 '24 19:02 dlevs