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

Check for leap year when calculating differenceInMonths involving February #3685

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

kaunartist
Copy link

@kaunartist kaunartist commented Jan 25, 2024

Problem

When calling differenceInMonths from January 31st to February 28th on a leap year, the logic for handling February does not account for the 29th. As a result intervalToDuration returns a duration of 1 month, -1 day.

Example

> differenceInMonths(new Date(2023, 1, 28), new Date(2023, 0, 31))
1
> differenceInMonths(new Date(2024, 1, 28), new Date(2024, 0, 31))
1
> intervalToDuration({ start: new Date(2023, 0, 31), end: new Date(2023, 1, 28) })
{ months: 1 }
> intervalToDuration({ start: new Date(2024, 0, 31), end: new Date(2024, 1, 28) })
{ months: 1, days: -1 }
> intervalToDuration({ start: new Date(2024, 0, 31), end: new Date(2024, 1, 29) })
{ months: 1 }

Expected Output

> differenceInMonths(new Date(2023, 1, 28), new Date(2023, 0, 31))
1
> differenceInMonths(new Date(2024, 1, 28), new Date(2024, 0, 31))
0
> intervalToDuration({ start: new Date(2023, 0, 31), end: new Date(2023, 1, 28) })
{ months: 1 }
> intervalToDuration({ start: new Date(2024, 0, 31), end: new Date(2024, 1, 28) })
{ days: 28 }
> intervalToDuration({ start: new Date(2024, 0, 31), end: new Date(2024, 1, 29) })
{ months: 1 }
  • Ran tests
  • Ran linter

@kaunartist kaunartist marked this pull request as ready for review January 25, 2024 18:06
@BenediktKa
Copy link

This issue is also affecting formatDistance, when using 1st of February 2024 and 1st of March 2024. This is due to the dependence of the differenceInMonths() as we noticed today in one of our test suits.
image

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

Successfully merging this pull request may close these issues.

None yet

2 participants