Get the number of business days between two dates (like networkdays in Excel)

Hi there, folks…

Long story short… I was trying to do what the title of my post says, and a pretty thorough search of the forum turned up posts with no answers (Calculate days from a date range - Vacation planning) or the solution that @romanmg wrote up here: Calculate Working Days (Business Days) between a Date Range

Well, for one reason or another, I didn’t want to use Air Date/Time Picker’s multiple dates or date range functionality, so romanmg’s solution wouldn’t work for me. That being said, I came up with my own solution that doesn’t seem like it’s the hackiest thing in the world, and I thought I would share it just in case it could be useful to someone else.

The solution is simply the number of days between two dates minus an “offset” that is calculated as follows: (the week number associated with the end date of the range - the week number associated with the start date of the range) * 2

So, you are getting the number of weeks between two dates and multiplying it by 2 because for every week, there are 2 days (1 Saturday and 1 Sunday, of course) you need to subtract from the total number of days between the dates.

Example:
Assume a first date of Jan. 9, 2019 and a second date of Jan. 25 2019.
The number of actual days between the two dates is 16.
The offset is calculated as Jan. 25 2019’s week number (4) - Jan. 9 2019’s week number (2) * 2 = 4 (meaning there are 4 weekend days between those two dates)
Therefore, the number of business days between the two dates is 16 - 4 = 12

True, this solution wouldn’t work across years, but that’s not a use case I am trying to cover right now.

Hopefully I haven’t missed something completely obvious that makes this solution a non-starter, but it appears to be working and like I said, it doesn’t seem like it’s the hackiest thing in the world (to me, at least). So, if it can be useful to someone else, well, there you go.

Best…

Mike

1 Like