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

Add new feature differenceInBusinessDays #1194

Merged

Conversation

codinsonn
Copy link
Contributor

Sister PR to #1154

Should partly solve #584. Allthough it does not take holidays into consideration, it does exclude weekends and should have some value for end users.

All in all, it is just a thin wrapper around eachDayOfInterval, filtering out the weekends though.

Example usage:

var amountOfBusinessDays = differenceInBusinessDays(
    new Date(2018, 0, 1),
    new Date(2019, 0, 1)
)
// => 261 (52 weekends excluded = 104 days skipped)

@kossnocorp
Copy link
Member

That sounds great, thank you! Please rebase with master so I can review the diff.

@codinsonn codinsonn force-pushed the feature/differenceInBusinessDays branch from afcee36 to 238457e Compare June 10, 2019 07:36
Copy link
Member

@kossnocorp kossnocorp left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For consistency, the first argument should be the later date: difference(4, 2) // => 2 and difference(2, 4) // => -2.

@kossnocorp
Copy link
Member

I'll finish the PR.

- Swap arguments to make it consistent with `differenceInDays` and
  `differenceInCalendarDays`.

- Substruct 1 from the result as the interval contains both starting
  and ending dates.

- Rework the edge cases.

- Improve the documentation.
@kossnocorp
Copy link
Member

Please note that I've changed the function behavior:

  • I've swapped the order of arguments, so the later date comes first. Now it's consistent with differenceInDays and differenceInCalendarDays
  • For the sake of consistency, the function now also returns a negative number if the earlier date comes first.
  • The function returns Invalid Date if any of the arguments is invalid.
  • I subtract 1 from the length of the interval because it contains both starting and ending dates. The difference between two dates should not be 2

Copy link
Member

@kossnocorp kossnocorp left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's done!

@kossnocorp kossnocorp merged commit b8d9ea1 into date-fns:master Jun 10, 2019
@kossnocorp
Copy link
Member

This PR was released as v2.0.0-alpha.32: https://gist.github.com/kossnocorp/a307a464760b405bb78ef5020a4ab136#v200-alpha32

elmomalmo pushed a commit to elmomalmo/date-fns that referenced this pull request Jul 12, 2019
@ro-savage
Copy link

@kossnocorp - great work release this. We've been using a custom function with quite similar code in our code base. However, we also take into account 'holidays' by allowing an array of excluded dates to be passed.

Would you be interested in a PR that allows people to add in an array of dates (holidays) to be excluded from differenceInBusinessDays?

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

Successfully merging this pull request may close these issues.

3 participants