1

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)

  • I was under the impression that SO questions were more language specific. I'm using a very naive tool for this and need a framework to think about it. I could easily do this with pandas, or copy something like this: stackoverflow.com/questions/1828948/… – Liam Foley May 17 '16 at 20:26
  • Many languages and frameworks provide you ways to perform date based math. Even database query languages. – Adam Zuckerman May 17 '16 at 20:29
  • The tricky part is the "business days" part. Python, for instance, doesn't have a standard way to do that from what I can tell. – Steven Burnap May 17 '16 at 20:31
  • 2
    In theory, this is possible by inventing some rule about what a business day is such as "any day that's not Saturday or Sunday". In practice, when you have to account for all of the real-world holidays that aren't on weekends, only get observed in some parts of the world and not others, and are all affected by time zones and DSTs, it's impossible to get anything perfectly right without a bunch of lookup tables. – Ixrec May 17 '16 at 21:08
  • 1
    All good points, but for my specific definition, we can assume all timestamps/dates are in UTC and we ca round down all timestamps to nearest day and assume M-F are business days and Sat-Sun are not. – Liam Foley May 18 '16 at 2:49
1

One of the tricky things about this is that '2016-05-06' was 4 business days after '2016-05-02', but '2016-05-10' was 2 business days after '2016-05-06', 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 y 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.

0

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.