Date and time
| CURRENT-DATE | |
| Date arithmetic | |
| Formatting |
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.
Next: JSON and XML
Article author: Arthur Isaev