To exclude weekends you cannot just subtract 2 for every week between the two dates, since it’ll also depend on whether the first/last dates lie on a weekend. So you’ll need to determine whether the first and/or last day are Saturday/Sunday. Have a look at this posting; although it is about weeks, it describes the tools necessary:
In particular, format-date(${date},"%a") will give you the day-of-week for a date (from which you can determine the needed Sat/Sun…)
Good luck. Please post your solution here when you’ve solved it.
It works but not for all cases. For example if the weekend(s) exists between the start and end dates, then the above code doesn’t work. Would you please check?
Psuedo code (ie you still need to fully translate this algorithm to XLSForm…):
day1 = int(decimal-date-time(${date1})
day2 = int(decimal-date-time(${date2})
// adjust start & end dates if they fall on a weekend
if format-date(${date1},"%a") = “Sat” then day1 += 2
else if format-date(${date1},"%a") = “Sun” then day1 += 1
if format-date(${date2},"%a") = “Sat” then day1 -= 1
else if format-date(${date2},"%a") = “Sun” then day1 -= 2
// determine number of weekends
delta = ${day2} - ${day1}
weeks = int(${delta} div 7)
// remove weekends
delta_business = ${delta} - (${weeks} * 2)
There are some edge cases - eg start on Saturday and end the next day (Sunday), but presumably these are not permitted…
@Xiphware Thanks for writing the algorithm. I have translated it into the xlsform code shown in the image, but sometimes it doesn’t work, IS there any bug in my xlsform translation? (For example 20 August 2021 to 02 September 2021) it prints out 11 days (it should be 5 business days).
@ahaiba, would you mind sharing your xlsform with the community instead of sharing the screenshot? Maybe the community could add to your xlsform and carry it forward.
This is the xlsform. please tell me where is the bug in calculating business days. (For example 20 August 2021 to 02 September 2021) it prints out 11 days (it should be 5 business days).
My error, there’s a bug in my algorithm as it wont always pickup whether there is a weekend between 2 dates; eg Thur thru following Wed is <7 days (so weeks=0), but there is still a weekend between them. So it could be a week out. I’ll fiddle around with it this weekend and see what I can come up with…
BTW, you may want to be precise as to what you define as ‘between’ (specifically, inclusiveness)? eg is start=Mon and end=Fri count as 4 business days, or 5?
…NETWORKDAYS includes both the start date and end date when calculating workdays. If you give NETWORKDAYS the same date for start date and end date, and the date is not a weekend or holiday, it will return 1.