How can I calculate the number of buisness days (M-F, agnostic of Holidays) between two dates without a look up table?
There has to be some sort of algorithm based on the number of all days and the starting date, right?
Another Way of Phrasing it:
If I have a number X, how can I tell how many times multiples of
6 + 7Y or
7 + 7Y occur in the range between
0 & X, where
Y = integers between 0 and X/7. There has to be a way using modulo or floor division, Correct?
f(17) = 4 (count of 6,7,13,14)
One of the tricky things about this is that
'2016-05-06' was 4 business days after
'2016-05-10' was 2 business days after
even though in both pairs of dates exactly the same total number of days passed.
So what I’d probably do to get the number of business days between date A
and date B (assuming “business day” means “any day that is not a
Saturday or Sunday”)
would be to choose a “zero date”, some Sunday before any date that
I would ever want to use as date A, find the number of business days between
the zero date and date B, and then subtract the number of
business days between the zero date and date A.
(Of course this is all for a certain definition of “between.”
For other definitions I might use a Monday as a zero date,
or use Monday with date A and Sunday with date B, or vice versa,
whatever gave results that fit the desired definition.)
One way to find the number of business days between
a zero-date Sunday and date X is to take the total number of days
between those dates and subtract the number of Saturdays and Sundays.
For this we could use what many call “integer division.”
Different programming languages give you different ways to indicate
that you want to divide one integer
x by another integer
while discarding the remainder. I’ll write this operation
x // y
for the purpose of this answer; substitute the correct form in whichever
language you use.
Let the integer
x be the total number of days between the
zero date (a Sunday) and date X.
Then the number of Sundays between the zero date and date X is
x // 7,
and the number of Saturdays is
(x + 1) // 7.
The number of business days then is
f(x) = x - (x // 7) - ((x + 1) // 7)
Note that integer division doesn’t follow the same distributive laws
as floating-point division (almost) does;
x - ((2*x + 1) // 7) would produce a lot of wrong answers.
The number of business days between date A and date B, which are respectively
a days and
b days after the zero date, is then
f(b) - f(a)
If you have to use Monday as a zero date then the number of business days
before date X, which is
x days after the zero date, is
f(x) - 1.
Compute the day of week for each date.
Most languages give you a function to find the difference in days between the two dates (even Python).
With those you can determine the number of weeks, and subtract the number of weekends.
For example, we could compute the number of weeks between the Saturday following the first date and the Saturday following the second date by starting with the difference in days and divide by 7 to get weeks.