CURRENT-DATE and date arithmetic

COBOL has no date type. A date is a number of eight digits and a time is a string, and the compiler gives one function for the clock of the run and three for the arithmetic of dates. That is enough for the two questions a batch job asks: what day is it, and how many days is that from the one in the file.

FUNCTION CURRENT-DATE

CURRENT-DATE returns twenty-one characters: YYYYMMDDHHMMSS, two digits of hundredths of a second, then the offset of the machine time zone. It is a string, so a program cuts it with reference modification:

IDENTIFICATION DIVISION. PROGRAM-ID. D1. DATA DIVISION. WORKING-STORAGE SECTION. 01 WS-NOW PIC X(21). 01 WS-DATE PIC X(8). 01 WS-TIME PIC X(8). 01 WS-YEAR PIC 9(4). 01 WS-MONTH PIC 9(2). 01 WS-DAY PIC 9(2). PROCEDURE DIVISION. MOVE FUNCTION CURRENT-DATE TO WS-NOW. DISPLAY "now " WS-NOW. MOVE WS-NOW(1:8) TO WS-DATE. MOVE WS-NOW(9:6) TO WS-TIME. DISPLAY "date " WS-DATE " time " WS-TIME. MOVE WS-NOW(1:4) TO WS-YEAR. MOVE WS-NOW(5:2) TO WS-MONTH. MOVE WS-NOW(7:2) TO WS-DAY. DISPLAY "year " WS-YEAR " month " WS-MONTH " day " WS-DAY. STOP RUN.

cobc -x -free -Wall -o d1 d1.cob
./d1

now 2026111810540609+0000
date 20261118 time 105406
year 2026 month 11 day 18

The run was made at 10:54:06.09 on 18 November 2026, and the offset +0000 is the UTC of the container. The eight characters at position 1 are the date, the six at position 9 are the time, and the two at position 15 are the hundredths that the printout above does not show.

WHEN-COMPILED has the same twenty-one-character shape and answers with the moment of the build, which the next program prints. A report that has to say when it was made uses CURRENT-DATE; a program that has to say which version of itself produced a file uses WHEN-COMPILED.

Days between two dates

INTEGER-OF-DATE turns an eight-digit date into a day number, and DATE-OF-INTEGER turns a day number back into a date. Subtraction and addition of day numbers is then the whole of date arithmetic, leap years included:

IDENTIFICATION DIVISION. PROGRAM-ID. D2. DATA DIVISION. WORKING-STORAGE SECTION. 01 WS-FROM PIC 9(8) VALUE 20261118. 01 WS-TO PIC 9(8) VALUE 20270101. 01 WS-DAYS PIC S9(9). 01 WS-BASE PIC 9(8) VALUE 20261118. 01 WS-LATER PIC 9(8). 01 WS-TARGET PIC 9(8) VALUE 20261231. PROCEDURE DIVISION. COMPUTE WS-DAYS = FUNCTION INTEGER-OF-DATE(WS-TO) - FUNCTION INTEGER-OF-DATE(WS-FROM) DISPLAY "days to new year " WS-DAYS. COMPUTE WS-LATER = FUNCTION DATE-OF-INTEGER( FUNCTION INTEGER-OF-DATE(WS-BASE) + 45) DISPLAY "45 days later " WS-LATER. COMPUTE WS-DAYS = FUNCTION INTEGER-OF-DATE(WS-TARGET) - FUNCTION INTEGER-OF-DATE(WS-BASE) DISPLAY "days to the 31st " WS-DAYS. DISPLAY "when compiled " FUNCTION WHEN-COMPILED. STOP RUN.

cobc -x -free -Wall -o d2 d2.cob
./d2

days to new year +000000044
45 days later 20270102
days to the 31st +000000043
when compiled 2026111810541606+0000

From 18 November to 1 January are 44 days, and 45 days after 18 November is 2 January, so the two answers agree. The count to 31 December is 43, which is one day less: the end date is not counted. The check is worth making for every date function, because the off-by-one of a period is the classic report bug of the end of a month.

The functions know about leap years: a period from February to March of a leap year has one day more and the day numbers carry it. The exercise at the end of the page asks for that run.

A date that does not exist

30 February is a number that fits PIC 9(8) and is not a date. The function does not complain about it:

IDENTIFICATION DIVISION. PROGRAM-ID. D3. DATA DIVISION. WORKING-STORAGE SECTION. 01 WS-BAD PIC 9(8) VALUE 20260230. 01 WS-DAYS PIC S9(9). PROCEDURE DIVISION. COMPUTE WS-DAYS = FUNCTION INTEGER-OF-DATE(WS-BAD) DISPLAY "int " WS-DAYS. DISPLAY "back " FUNCTION DATE-OF-INTEGER(WS-DAYS). STOP RUN.

cobc -x -free -Wall -o d3 d3.cob
./d3

int +000000000
back 00000000

Zero, and the reverse of zero is the eight zeros of a date that is not a date either. A job that reads dates from a file therefore validates them before it converts them: month between 1 and 12, day between 1 and the last day of that month, and the year in the range the file claims. The file errors lesson is where the rejected record and its message go.

Exercises

  1. Print the hundredths of a second of CURRENT-DATE with reference modification.
  2. Count the days from 1 February 2028 to 1 March 2028 and from the same days of 2027. What is the difference?
  3. Add 365 days to 1 January 2027 with the two functions and print the result.
  4. Write a validation paragraph for a date and test it on 20260230, 20261301 and 20261231.

Questions

A new set of questions in a new order every time. Choose one answer for each, then check.

  1. How many characters does FUNCTION CURRENT-DATE return?

    Answer

    Twenty-one: the date, the time, the hundredths and the time-zone offset

    The run printed 2026111810540609+0000, which is 21 characters.

  2. What does INTEGER-OF-DATE return?

    Answer

    A day number that can be added and subtracted

    The difference of two day numbers gave the days between the dates.

  3. What does DATE-OF-INTEGER do?

    Answer

    It turns a day number back into an eight-digit date

    Day number plus 45 came back as 20270102.

  4. What does INTEGER-OF-DATE give for 30 February 2026?

    Answer

    Zero, without an error

    The run printed int +000000000 and the reverse gave eight zeros.

  5. What does this program print? IDENTIFICATION DIVISION. PROGRAM-ID. Q5. DATA DIVISION. WORKING-STORAGE SECTION. 01 W-D PIC 9(8) VALUE 20261231. PROCEDURE DIVISION. DISPLAY FUNCTION DATE-OF-INTEGER(FUNCTION INTEGER-OF-DATE(W-D) + 1). STOP RUN.

    Answer

    20270101

    One day after the last day of 2026 is the first day of 2027.

  6. What does this program print? IDENTIFICATION DIVISION. PROGRAM-ID. Q6. DATA DIVISION. WORKING-STORAGE SECTION. 01 W-A PIC 9(8) VALUE 20270101. 01 W-B PIC 9(8) VALUE 20270131. 01 W-N PIC 9(3). PROCEDURE DIVISION. COMPUTE W-N = FUNCTION INTEGER-OF-DATE(W-B) - FUNCTION INTEGER-OF-DATE(W-A) DISPLAY W-N. STOP RUN.

    Answer

    030

    From 1 January to 31 January is thirty days, the end date not counted.

  7. What does this program print? IDENTIFICATION DIVISION. PROGRAM-ID. Q7. DATA DIVISION. WORKING-STORAGE SECTION. 01 W-NOW PIC X(21) VALUE "2026111810540609+0000". PROCEDURE DIVISION. DISPLAY W-NOW(1:8). STOP RUN.

    Answer

    20261118

    The first eight characters of the function are the date.

  8. What does this program print? IDENTIFICATION DIVISION. PROGRAM-ID. Q8. DATA DIVISION. WORKING-STORAGE SECTION. 01 W-M PIC 9(2) VALUE 13. 01 W-D PIC 9(2) VALUE 1. 01 W-OK PIC X VALUE "Y". PROCEDURE DIVISION. IF W-M IS GREATER THAN 12 MOVE "N" TO W-OK END-IF. DISPLAY W-OK. STOP RUN.

    Answer

    N

    Month 13 is outside the range, which is the first check a date validator makes.

  9. Which function tells a report when the program that produced it was compiled?

    Answer

    FUNCTION WHEN-COMPILED

    The second program printed a build timestamp with it.

  10. Why does a job validate a date before it converts it?

    Answer

    The conversion of an impossible date is zero and raises nothing

    Thirty February gave zero and the reverse of zero gave eight zeros.

The code on this page was built with cobc (GnuCOBOL) 3.2.0 and run on Alpine Linux 3.24.3 (x86_64) in a Docker container.

Article author: Arthur Isaev