COBOL User Groups COBOL COBOL Programming
Home | Search | Join COBUG | Call for User Group Leaders | Contact | About



COBOL Community Code Sample Contribution

Contributed by: Don Stafford, Director of Information Technologies, UAV Entertainment Corporation

We needed a routine to calculate a date in the future, based on business days, not on calendar days.

In searching the internet, no COBOL source could be found.
However, several routines were found in various other programming languages.
I took several of those and adapted the code to MicroFocus Cobol.

In Working Storage, you need the following variables:

      01 CalDays                         PIC 9(3).
      01 BusDays                        PIC 9(3).
      01 DelDay                          PIC 9(3).
      01 DelWks                         PIC 9(3).
      01 WS-DAY-OF-WEEK        PIC 9(2).
      01 DAYS-TO-ADD              PIC 9(3).
      01 DATE-YMD-IN               PIC 9(8).
      01 DATE-YMD-OUT            PIC 9(8).
      01 DATE-DAY                    PIC 9(8).

In the Procedure Division, you have 2 paragraphs:

      ADD-BUS-DAYS-TO-DATE.

         PERFORM GET-DAY-OF-WEEK-FOR-DATE.
         IF WS-DAY-OF-WEEK = 6
            MOVE 5 TO WS-DAY-OF-WEEK
            SUBTRACT 1 FROM DAYS-TO-ADD.

         MOVE DAYS-TO-ADD TO CalDays,
                                                        BusDays.

         COMPUTE DelDay = WS-DAY-OF-WEEK + DAYS-TO-ADD.

         IF DelDay > 5
            COMPUTE BusDays = BusDays - ( 6 - WS-DAY-OF-WEEK )
            COMPUTE CalDays = CalDays + 2
            COMPUTE DelWks = BusDays / 5
            COMPUTE DAYS-TO-ADD = CalDays + (DelWks * 2).

          PERFORM ADD-DAYS-TO-DATE.

      ADD-DAYS-TO-DATE.

         COMPUTE DATE-DAY = FUNCTION INTEGER-OF-DATE (DATE-YMD-IN).
         ADD DAYS-TO-ADD TO DATE-DAY.
         COMPUTE DATE-YMD-OUT = FUNCTION DATE-OF-INTEGER (DATE-DAY).

The current (base) date is moved into DATE-YMD-IN.
The number of (business) days to add is moved into DAYS-TO-ADD.
You then call  ADD-BUS-DAYS-TO-DATE.
The resulting date is in DATE-YMD-OUT.

Obvious note:  If you call ADD-DAYS-TO-DATE, it simply calculates a date in the future without regards to weekends.
Not-so-obvious note:  If you make ADD-DAYS-TO-DATE a PIC S9(3), then ADD-DAYS-TO-DATE will add or subtract days.  The ADD-BUS-DAYS-TO-DATE only works with +(positive) numbers.


------------------------------------------------------------------------
Don Stafford, Director of Information Technologies
UAV Entertainment Corporation
2200 Carolina Place
Fort Mill, SC  29708



[ Go to Top of Page ]


Return to Code Samples page.

COBOL (c) Information Computing Services. All Rights Reserved. COBOL