COBOL Date Arithmetic with INTEGER-OF-DATE and DATE-OF-INTEGER
Use COBOL intrinsic functions to convert YYYYMMDD dates to integer day counts and back, avoiding manual date math in batch programs.
04 May 2026, 02:44 UTC

The practical answer
If a batch COBOL program must compute the number of days between two dates, or add days to a date, do not write manual month-length logic. Use the COBOL intrinsic functions FUNCTION INTEGER-OF-DATE and FUNCTION DATE-OF-INTEGER. An intrinsic function is a built-in operation supplied by the compiler. INTEGER-OF-DATE converts a date in YYYYMMDD form into an integer day count; DATE-OF-INTEGER converts that integer back into YYYYMMDD form. Subtracting two day counts gives a reliable date difference without calendar tables.
These functions are part of the COBOL 2002 standard and appear in GnuCOBOL, Micro Focus COBOL, and IBM Enterprise COBOL. Exact spelling, return format, and default century behavior can differ by compiler, so verify against your compiler's user guide before putting the code in production.
How the conversion works
The program passes an eight-digit numeric date to INTEGER-OF-DATE. The compiler returns an integer whose meaning depends on the compiler's base date; some implementations use a Lilian day count, while others use a different epoch. Because the base can vary, treat the integer as an opaque value for differences and round trips within the same compiler, not as a universal calendar serial number.
A century window is the compiler rule that decides which century a two-digit year belongs to. If you always pass full four-digit years in YYYYMMDD format, the window matters less, but it can still affect how the compiler validates or stores the input.
Data division declarations
DATA DIVISION.
WORKING-STORAGE SECTION.
01 WS-INPUT-DATE PIC 9(8).
01 WS-DAY-COUNT PIC 9(10).
01 WS-OUTPUT-DATE PIC 9(8).
01 WS-DATE-ONE PIC 9(8).
01 WS-DATE-TWO PIC 9(8).
01 WS-DAYS-BETWEEN PIC S9(10) SIGN TRAILING SEPARATE.
PIC 9(8) defines an eight-digit numeric field. PIC S9(10) defines a signed ten-digit field large enough to hold a day count and the result of a subtraction.
Procedure division example
PROCEDURE DIVISION.
MAIN-LOGIC.
MOVE 20261018 TO WS-INPUT-DATE.
*> Convert calendar date to integer day count.
COMPUTE WS-DAY-COUNT = FUNCTION INTEGER-OF-DATE(WS-INPUT-DATE)
DISPLAY "DAY-COUNT: " WS-DAY-COUNT.
*> Convert the integer back to calendar form.
COMPUTE WS-OUTPUT-DATE = FUNCTION DATE-OF-INTEGER(WS-DAY-COUNT)
DISPLAY "ROUND-TRIP: " WS-OUTPUT-DATE.
*> Difference between two dates.
MOVE 20261001 TO WS-DATE-ONE.
MOVE 20261018 TO WS-DATE-TWO.
COMPUTE WS-DAYS-BETWEEN =
FUNCTION INTEGER-OF-DATE(WS-DATE-TWO)
- FUNCTION INTEGER-OF-DATE(WS-DATE-ONE).
DISPLAY "DAYS-BETWEEN: " WS-DAYS-BETWEEN.
GOBACK.
The asterisk lines are comments. The COMPUTE statement evaluates an expression; intrinsic functions can appear on the right side of COMPUTE or in a DISPLAY statement, but they cannot be used as a receiving field in a MOVE statement.
Computing a future or past date
*> Add ten days to the input date.
COMPUTE WS-DAY-COUNT =
FUNCTION INTEGER-OF-DATE(WS-INPUT-DATE) + 10.
COMPUTE WS-OUTPUT-DATE = FUNCTION DATE-OF-INTEGER(WS-DAY-COUNT).
DISPLAY "PLUS-TEN-DAYS: " WS-OUTPUT-DATE.
Month rollover and leap years are handled by the compiler's conversion routine, not by handwritten IF logic.
Compile and run
Run the compiler from a terminal on the build host. You need read and write permission for the source directory and execute permission for the directory where the binary is written.
$ cobc -x -free date-conversion.cob -o date-conversion
$ ./date-conversion
Replace date-conversion.cob with your source filename. The -free flag tells GnuCOBOL to expect free-format source; omit it for traditional fixed-format COBOL. Expected checks: the compiler returns exit status zero, and the program prints a day count, a round-trip date matching the input, and a day difference. Risks: the compiler may not be installed, fixed-format source may reject the *> comment style, and century-window settings can change results for two-digit year input.
Limits and common mistakes
- Do not assume
FUNCTION CURRENT-DATEreturns the same format on every compiler; some returnYYYYMMDDwhile others return a longer timestamp. - Avoid mixing intrinsic date functions with two-digit year fields in the same computation. A two-digit year can be interpreted differently depending on the active century window.
- Intrinsic functions cannot be used as receiving fields in
MOVE. UseCOMPUTEorDISPLAYinstead. INTEGER-OF-DATEexpectsYYYYMMDD. Passing an invalid date such as20261301may return zero or a compiler-dependent value.- The integer day count is not portable across compilers unless the documentation defines a common epoch. Use it for differences and round trips within one compiler toolchain.
Verify before you rely on it
Compile a small program that displays FUNCTION INTEGER-OF-DATE(20261018) and compare the output to the day count documented for your compiler. Check the compiler user guide for the supported intrinsic function list and the century window configuration. If you have access to more than one compiler, run the same source under GnuCOBOL and Micro Focus to confirm function name spelling and return format. Do not copy output from another system into your own documentation; regenerate it on your target compiler.
Version assumptions
This guide assumes a compiler implementing COBOL 2002 intrinsic date functions, such as GnuCOBOL, Micro Focus COBOL, or IBM Enterprise COBOL. Older COBOL compilers may not support these functions at all. Confirm the function names in your compiler's reference manual before adopting the pattern.
0 replies
A thoughtful contribution can make all the difference. Be the first to share one.