Sample COBOL DATE Program for Mainframe Projects

COBOL sample program on how to use DATE concepts. You can use this program for your reference.

IDENTIFICATION DIVISION.
PROGRAM-ID. DAYS-BETWEEN.
ENVIRONMENT DIVISION.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 DIFF-DAYS    PIC +++,+++,++9.
01 TOTAL-DAYS1  PIC S9(8) BINARY.
01 EDITED-DATE1 PIC X(20).
01 DATE1.
03 YY     PIC 99.
03 MM     PIC 99.
03 DD     PIC 99.
01 TOTAL-DAYS2  PIC S9(8) BINARY.
01 EDITED-DATE2 PIC X(20).
01 DATE2.
03 YY     PIC 99.
03 MM     PIC 99.
03 DD     PIC 99.
01 INPUT-DATE.
03 YY     PIC 99.
03 MM     PIC 99.
03 DD     PIC 99.
01 DATE-OK     PIC 9.
88 INVALID-DATE VALUE IS 1.
01 EDITED-DATE.
03 MM     PIC XXXB.
03 DD     PIC Z9,B.
03 YY     PIC 9(4).
01 TOTAL-DAYS   PIC S9(8) BINARY.
77 CTR          PIC S9(4) BINARY.
77 LEAP-YEAR    PIC S9(8) BINARY.
77 WORK-YEAR    PIC S9(4) BINARY.
77 REM          PIC S9(4) BINARY.
01 MMDD-TABLE.
03 DAYS     PIC X(24)
VALUE  “312831303130313130313031”.
03 MMDD     REDEFINES DAYS
PIC 99 OCCURS 12 TIMES.
01 MONTH-NAMES.
03 MONTHS   PIC X(36)
VALUE “JanFebMarAprMayJunJulAugSepOctNovDec”.
03 M-NAME   REDEFINES MONTHS
PIC XXX OCCURS 12 TIMES.
PROCEDURE DIVISION.
**Get first date
GET-FIRST-DATE.
DISPLAY “Enter first date (YYMMDD):”
ACCEPT DATE1.
MOVE DATE1 TO INPUT-DATE.
**Check date and compute number of days in first date
PERFORM DAYS-FROM-DATE THRU DAYS-FROM-DATE-EXIT.
IF INVALID-DATE
DISPLAY “Invalid Date!”
GO TO GET-FIRST-DATE.
MOVE EDITED-DATE TO EDITED-DATE1.
MOVE TOTAL-DAYS TO TOTAL-DAYS1.
GET-SECOND-DATE.
DISPLAY “Enter second date (YYMMDD):”
ACCEPT DATE2.
MOVE DATE2 TO INPUT-DATE.
**Compute number of days in second date
PERFORM DAYS-FROM-DATE THRU DAYS-FROM-DATE-EXIT.
IF INVALID-DATE
DISPLAY “Invalid Date!”
GO TO GET-SECOND-DATE.
MOVE EDITED-DATE TO EDITED-DATE2.
MOVE TOTAL-DAYS TO TOTAL-DAYS2.
SUBTRACT TOTAL-DAYS1 FROM TOTAL-DAYS2 GIVING DIFF-DAYS
DISPLAY “Date 1: “, EDITED-DATE1
DISPLAY “Date 2: “, EDITED-DATE2
DISPLAY “Difference in Days: “ DIFF-DAYS.
TERM-PROC.
EXIT PROGRAM.
**Check date is valid and compute the days since 01/01/1900
DAYS-FROM-DATE.
**Check that INPUT-DATE is legal
MOVE 0 TO DATE-OK.
IF YY OF INPUT-DATE IS NOT NUMERIC OR
MM OF INPUT-DATE IS NOT NUMERIC OR
DD OF INPUT-DATE IS NOT NUMERIC OR
MM OF INPUT-DATE IS LESS THAN 1 OR
MM OF INPUT-DATE IS GREATER THAN 12 OR
DD OF INPUT-DATE IS LESS THAN 1
MOVE 1 TO DATE-OK,
GO TO DAYS-FROM-DATE-EXIT.
**Compute the days as years * 365
COMPUTE TOTAL-DAYS = YY OF INPUT-DATE * 365.
**Then add 1 for each leap year
COMPUTE LEAP-YEAR  = (YY OF INPUT-DATE - 1) / 4.
COMPUTE TOTAL-DAYS = TOTAL-DAYS + LEAP-YEAR.
**Compute the number of days in February for the final year
DIVIDE YY OF INPUT-DATE BY 4 GIVING WORK-YEAR REMAINDER REM.
IF REM = 0 THEN
MOVE 29 TO MMDD(2)
ELSE
MOVE 28 TO MMDD(2).
**Check that days in month is legal
IF DD OF INPUT-DATE IS GREATER THAN MMDD(MM OF INPUT-DATE)
MOVE 1 TO DATE-OK,
GO TO DAYS-FROM-DATE-EXIT.
**Add number of days in each month up to the last
PERFORM VARYING CTR FROM 1 BY 1 UNTIL CTR = MM OF INPUT-DATE
COMPUTE TOTAL-DAYS = TOTAL-DAYS + MMDD(CTR)
END-PERFORM.
**Add the days in the last month
ADD DD OF INPUT-DATE TO TOTAL-DAYS
**Produce an edited form of the input date
MOVE DD OF INPUT-DATE TO DD OF EDITED-DATE.
ADD 1900, YY OF INPUT-DATE GIVING YY OF EDITED-DATE.
DAYS-FROM-DATE-EXIT.
EXIT.
END PROGRAM DAYS-BETWEEN.
Advertisements

Author: Srini

Experienced software developer. Skills in Development, Coding, Testing and Debugging. Good Data analytic skills (Data Warehousing and BI). Also skills in Mainframe.