Domino Code Fragment

Code Name*
Calculating Business Days Between Two Dates
Date*
04/28/2024
Source (or email address if you prefer)*
Rlatulippe@romac.com
IP address:.18.221.239.148
Description*
It is elementary for a Notes programmer to calculate the number of calendar days between two dates (It is even quite easy to calculate the non-weekend days between two dates. However, it is much tougher to calculate the number of business days given a list of holidays. This requires some form of recursion or looping to check 1) if a holiday falls anywhere within the range, or 2) if any of the dates in the range is on a list of holidays. The presumption is that you don't know exactly how many calculations you need to make, hence the need for recursion or looping. The following example demonstrates a recursive mechanism to do this calculation. It uses a button and three macros. Two dates are supplied. A button is avialable to calculate and set the value for a third field containing the number of business days. A text list containing the holidays must also be available, either in a variable or through a lookup.
Type*
Formula
Categories*
Date/Time Handling
Implementation:
Required Client:
Server:
Limitations:
Comments:
Files/Graphics attachments (if applicable): Code:
Button:

Check := @If(Startdate = ""; 1; EndDate = ""; 1; 0);
@If(Check; @Do(@Prompt([OK]; "No Values"; "You must enter starting and ending dates."); @Return("")); "");
HolidayList := @DbColumn(""; ""; "($Holidays)"; 2);
@SetField("Holidays"; HolidayList);
@Environment("HolidayList"; @Implode(HolidayList; ";"));
@Environment("Iterations"; @Text(((EndDate - StartDate) / (24 * 60 * 60)) + 1));
@Environment("StartDate"; @Text(StartDate));
@Environment("SlidingDate"; @Text(StartDate));
@Environment("EndDate"; @Text(EndDate));
@Environment("WorkDays"; "0");
@Command([FileSave]);
@Command([FileCloseWindow]);
@Command([ToolsRunMacro]; "($Add-A-Day)")

Execute-Once Macro "($Add-A-Day)":
REM "ADD-A-DAY Macro";
REM "Recursive macro to check if dates fall on weekends ";
REM "and holidays and increment a working day count.";
REM "Retrieve all the environment variable";
Iterations := @TextToNumber(@Environment("Iterations"));
Sdate := @TextToTime(@Environment("SlidingDate"));
EndDate := @TextToTime(@Environment("EndDate"));
WorkDays := @TextToNumber(@Environment("WorkDays"));
Holidays := @Explode(@Environment("HolidayList"); ";");
REM "Calculate Add a Day - will be zero or one";
AddDay := @If(@Weekday(Sdate) = 1; 0; @Weekday(Sdate) = 7; 0; Sdate > EndDate; 0; @Text(Sdate) = Holidays; 0; 1);
REM "Check if there are any more iterations to do";
REM "If not, call a macro to update the fields";
REM "If yes, then save the new values and call the recursion routine";
@If(Iterations = 0; @Do(@Command([ToolsRunMacro]; "($SetNumDaysField)"); @Command([EditDocument]; "1")); @Do(@Environment("Iterations"; @Text(Iterations - 1)); @Environment("SlidingDate"; @Text(@Adjust(Sdate; 0; 0; 1; 0; 0; 0))); @Environment("WorkDays"; @Text(WorkDays + AddDay)); @Command([ToolsRunMacro]; "($Recurs-Add)")));
SELECT @All

Execute-Once Macro "($Recurs-Add)":
@Command([ToolsRunMacro]; "($Add-A-Day)");
SELECT @All

Filter Macro "($Recurs-Add)":
FIELD NumDays := @Environment("WorkDays");
SELECT @All