Monday, September 26, 2011

COBOL:Subtract 6 months from a date



The below code snippet subtracts six months from a given 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....
ACCEPT WS-DATE

IF MM > 6 THEN
   SUBTRACT 6 FROM MM
ELSE
   ADD      6 TO   MM
   SUBTRACT 1 FROM YYYY
END-IF.

* 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.