Date and time
CURRENT-DATE
FUNCTION CURRENT-DATE returns 21 characters: YYYYMMDDHHMMSSmmmuuun. Move it into a field and slice the parts you need with reference modification:
01 WS-STAMP PIC X(21).
01 WS-YEAR PIC 9(4).
MOVE FUNCTION CURRENT-DATE TO WS-STAMP.
MOVE WS-STAMP(1:4) TO WS-YEAR.
Date arithmetic
INTEGER-OF-DATE turns a YYYYMMDD integer into a day count. Subtract two counts to get the days between dates, and DATE-OF-INTEGER converts back:
COMPUTE WS-DAYS = FUNCTION INTEGER-OF-DATE(WS-END)
- FUNCTION INTEGER-OF-DATE(WS-START).
COMPUTE WS-END = FUNCTION DATE-OF-INTEGER(WS-DAYNUM).
Formatting
For report headers, string the slices together with STRING. Keep raw timestamps in COMP-3 fields for math, and format only at display time so the report layer can change without touching the calculations.
STRING WS-YEAR "/" WS-STAMP(5:2) "/" WS-STAMP(7:2)
INTO WS-REPORT-DATE.
The code on this page was run with GnuCOBOL 3.2.0 on Alpine Linux 3.24.3 (x86_64) in a Docker container.
Article author: Arthur Isaev