dlevs / duration-fns

Functions for working with durations.
MIT License
58 stars 6 forks source link

Strange result for `.between` for Feb-March #33

Open forabi opened 7 months ago

forabi commented 7 months ago
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 commented 7 months ago

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

dlevs commented 7 months ago

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.