Monday, September 26, 2011

COBOL: Subtract 1 year from current date



The below code snippet explains how to subtract one year from the current date.


....Working Storage....

01 MONTH-END-DD PIC X(24) VALUE '312831303130313130313031'.
01 TBL-MONTH-END REDEFINES MONTH-END-DD.
   03 TBL-MONTH-END-DAY OCCURS 12 TIMES.
   05 TBL-MONTH-END-DD PIC 9(02).

01 WS-DATE.
   02 YYYY PIC 9(4).
   02 MM PIC 99.
   02 DD PIC 99.

....Procedure Division....

MOVE my-date to WS-DATE

SUBTRACT 1 FROM YYYY

* Leap year Check
IF ( FUNCTION MOD (YYYY, 400) = 0) OR
   ( FUNCTION MOD (YYYY, 4) = 0 AND
     FUNCTION MOD (YYYY, 100) NOT = 0 )
     MOVE '29' TO MONTH-END-DD (3:2)
ELSE
     MOVE '28' TO MONTH-END-DD (3:2)
END-IF

IF DD > TBL-MONTH-END-DD (MM)
   MOVE TBL-MONTH-END-DD (MM) TO DD
END-IF

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.