# Solved: Not a future date calculation

Calculating future dates is a common requirement in software development, particularly in business applications such as billing and scheduling systems. In languages such as Java or Python, this task could be straightforward due to inbuilt libraries and functions. However, in older languages such as Cobol, this task can prove to be more complex as we often have to take into account factors such as leap years.

IDENTIFICATION DIVISION.
PROGRAM-ID. FUTURE-DATES.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 WS-CURRENT-DATE.
05 WS-CURRENT-YEAR PIC 9(04).
05 WS-CURRENT-MONTH PIC 9(02).
05 WS-CURRENT-DAY PIC 9(02).
01 WS-FUTURE-DATE.
05 WS-FUTURE-YEAR PIC 9(04).
05 WS-FUTURE-MONTH PIC 9(02).
05 WS-FUTURE-DAY PIC 9(02).
PROCEDURE DIVISION.
ACCEPT WS-CURRENT-DATE FROM DATE
COMPUTE WS-FUTURE-DAY = WS-CURRENT-DAY + 7
MOVE WS-CURRENT-MONTH TO WS-FUTURE-MONTH
MOVE WS-CURRENT-YEAR TO WS-FUTURE-YEAR
IF WS-FUTURE-DAY > 30
SUBTRACT 30 FROM WS-FUTURE-DAY
END-IF
IF WS-FUTURE-MONTH > 12
SUBTRACT 12 FROM WS-FUTURE-MONTH
END-IF
DISPLAY “ONE-WEEK LATER DATE IS ” WS-FUTURE-DATE
STOP RUN.

Let’s break down this code.

## How is the code structured?

The Cobol program starts with the ‘IDENTIFICATION DIVISION’ which is a mandatory division in all Cobol programs. The ‘PROGRAM-ID’ statement following it identifies the program. The ‘DATA DIVISION’ is where we define all the data or variables that will be used in the program. ‘WORKING-STORAGE SECTION’ is a subdivision of the ‘DATA DIVISION’ where we declare our working variables: ‘WS-CURRENT-DATE’ and ‘WS-FUTURE-DATE’. Each of these is further broken down into day, month, and year portions. The ‘PROCEDURE DIVISION’ is where we code the logic for calculating the future date.

## The Concept of Date Manipulation in Cobol

Our Cobol program first accepts the current date from the system. It then calculates the future date by adding 7 to the current day. If the future day exceeds 30 (signifying the end of a month), it increments the month by 1 and subtracts 30 from the day. A similar process is used to account for the end of the year. Evidently, this is a simplified example that doesn’t take into account varying month lengths or leap years, but it outlines the basic process.

In conclusion, while dealing with dates in Cobol can at first seem difficult, it can be eased by breaking down the date into smaller components and carefully handling the transition between days, months, and years. Such careful handling of dates is also a reminder of the attention to detail required in Cobol, reflecting the language’s era of origin and its continued use in systems where precision and accuracy are paramount.

Related posts: