Skip to content

Add new feature differenceInBusinessDays#1194

Merged
kossnocorp merged 2 commits into
date-fns:masterfrom
codinsonn:feature/differenceInBusinessDays
Jun 10, 2019
Merged

Add new feature differenceInBusinessDays#1194
kossnocorp merged 2 commits into
date-fns:masterfrom
codinsonn:feature/differenceInBusinessDays

Conversation

@codinsonn
Copy link
Copy Markdown
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
Copy Markdown
Member

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

@kossnocorp kossnocorp added the 🎉 Feature #265d21 label Jun 10, 2019
@codinsonn codinsonn force-pushed the feature/differenceInBusinessDays branch from afcee36 to 238457e Compare June 10, 2019 07:36
Copy link
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown

@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

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants