How do I calculate the difference in working days between two dates?

With the getdaysdiff, I get the total day difference between two dates. But if I just want the working days, how can I achieve this?

That’s actually not as simple as I thought it would be. After a bit of experimenting I found that the following expression seems to work:

%start.getdaysto(%end).div(7).floor().mult(5)
.add(
 int:if(
  %end.getdayofweek().isgreaterequal(%start.getdayofweek()),
  int:min([%end.getdayofweek(),5]).sub(int:min([%start.getdayofweek(),6])),
  int:load(5).sub(int:min([%start.getdayofweek(),6])).add(int:min([%end.getdayofweek(),5]))
 )
)
.add(1)

NB: %start and %end should be dates (no time parts), %end should be greater than or equal to %start, and this counts the working days including the start and the end date. This means for example that when %start and %end are the same day, it returns 1 (for week days) or 0 (for weekend days).

What this does, is first count the working days in the full weeks between start and end date, then add days for the start week and the end week (which could be the same), and then add 1 to be inclusive.

A couple of disclaimers: I haven’t tested this extensively, so no guarantees - let me know if you find any issues and I will double check. Also, it’s probably not the most efficient way to make this calculation (but this way it remains more or less readable).

(By the way, in practice, when we count working/business days, we often also want to consider public holidays, and sometimes even personal leave requests, in which case it might be easiest to simply generate records for each (working) day and count them.)

In our absence module we created a process to calculate this.
This is the process ‘Get Number of working days based on a start and enddate’ in component 9335. The process is however not in the reference architecture.

1 Like

That seems like a good solution! Unfortunately, it seems I don’t have the rights to open your component. @Wim do you know a method for me to be able to check the process above? If not, I’ll use and test your expression, that also seems like it should work well.

I think the component that ArnoldT refers to is not owned by Novulo, so I can’t give you access.

I can however give you access to a small test component I created to calculate the number of business days. It implements the expression I described, as well as a process. That’s model number 10774.

In the process implementation I took a different approach and used the DateTimeGetOccurrence action from the DateTimeExtensions plug-in:

image

image

2 Likes