COBOL Tutorial

Progress0 of 0 lessons

COBOL Date-Time Functions

COBOL provides intrinsic functions for date and time: getting the current date and time (CURRENT-DATE), converting between dates and integers for arithmetic (INTEGER-OF-DATE, DATE-OF-INTEGER), and related functions. These help you avoid hand-written date logic for month boundaries and leap years. This page explains the main date-time functions and how to use them.

Explain Like I'm Five: Date-Time Functions

Date-time functions are helpers that answer questions like "what is today?" (CURRENT-DATE) or "what date is 30 days from this date?" (INTEGER-OF-DATE, add 30, DATE-OF-INTEGER). Instead of you counting days and months, the function does it. You give it a date (or nothing for "now") and get back a value you can store or use in calculations.

Common Date-Time Functions

Date-time intrinsic functions (availability varies by compiler)
FunctionArgumentsReturnsTypical use
CURRENT-DATENoneAlphanumeric (e.g. 21 chars)Get current date/time; substring for YYYYMMDD or HHMMSS
INTEGER-OF-DATEDate (YYYYMMDD format)Integer (day number)Convert date to integer for arithmetic
DATE-OF-INTEGERInteger (day number)Date (YYYYMMDD)Convert back to date after arithmetic
DAY-OF-INTEGERIntegerInteger (day of week or similar)Day-of-week from day number (implementation-dependent)
WHEN-COMPILEDNoneCompilation timestampVersion or build identification

CURRENT-DATE

FUNCTION CURRENT-DATE takes no arguments and returns the current date and time. The return format is implementation-defined; often it is a 21-character string where positions 1–4 are year, 5–6 month, 7–8 day, 9–10 hour, 11–12 minute, 13–14 second, and the rest may be hundredths and timezone. Extract the date with reference modification: MOVE FUNCTION CURRENT-DATE(1:8) TO WS-YYYYMMDD gives you an 8-character date. Extract time with (9:6) for HHMMSS.

cobol
1
2
3
4
5
6
7
8
01 WS-DATE-TIME PIC X(21). 01 WS-YYYYMMDD PIC X(8). 01 WS-HHMMSS PIC X(6). MOVE FUNCTION CURRENT-DATE TO WS-DATE-TIME *> Extract date (positions 1-8) and time (9-14 or 9-6 depending on format) MOVE WS-DATE-TIME(1:8) TO WS-YYYYMMDD MOVE WS-DATE-TIME(9:6) TO WS-HHMMSS

INTEGER-OF-DATE and DATE-OF-INTEGER

INTEGER-OF-DATE converts a date (typically in YYYYMMDD or similar format) to an integer that represents that date (e.g. days since a base date). DATE-OF-INTEGER converts that integer back to a date. The base date and exact argument/return formats are defined by your COBOL implementation. Use these for date arithmetic: convert to integer, add or subtract days, convert back. This correctly handles month and year boundaries and leap years.

cobol
1
2
3
4
5
6
7
8
*> Add 30 days to WS-START-DATE (PIC 9(8) YYYYMMDD) 01 WS-START-DATE PIC 9(8) VALUE 20240101. 01 WS-INT-DATE PIC S9(9). 01 WS-END-DATE PIC 9(8). COMPUTE WS-INT-DATE = FUNCTION INTEGER-OF-DATE(WS-START-DATE) + 30 MOVE FUNCTION DATE-OF-INTEGER(WS-INT-DATE) TO WS-END-DATE *> WS-END-DATE now holds the date 30 days later

Step-by-Step: Get Today and Format It

  • Move FUNCTION CURRENT-DATE to an alphanumeric field long enough for the return value (e.g. 21 characters).
  • Use reference modification to copy the date portion (e.g. 1:8) into a YYYYMMDD field and the time portion (e.g. 9:6) into an HHMMSS field if needed.
  • For display, build a formatted string (e.g. MM/DD/YYYY) using the extracted parts and STRING or separate MOVE statements.

Best Practices

  • Check your compiler documentation for the exact CURRENT-DATE format and for INTEGER-OF-DATE/DATE-OF-INTEGER argument and return types.
  • Use INTEGER-OF-DATE and DATE-OF-INTEGER for date arithmetic instead of manual day/month/year logic.
  • Use reference modification or subordinate items to extract date and time parts from CURRENT-DATE so the code is clear and maintainable.

Test Your Knowledge

1. How do you get the current date and time in COBOL?

  • ACCEPT FROM SYSTEM
  • FUNCTION CURRENT-DATE
  • MOVE TODAY TO WS-DATE
  • READ DATE-FILE

2. To add 7 days to a date, a common approach is:

  • Add 7 to the YYYYMMDD field directly
  • Use INTEGER-OF-DATE, add 7, then DATE-OF-INTEGER
  • Use CURRENT-DATE only
  • Use WHEN-COMPILED

Related Concepts

Related Pages