COBOL Intrinsic Date Functions

Home > COBOL Intrinsic Date Functions

The standard ACCEPT statement for getting the system date was a large part of the Y2K problem, since it only allowed for two-digit years. The 1989 revision of the COBOL standard introduced the following functions for doing date manipuation, all of which use four-digit years. Of course, these will not prevent a Y10K problem, but by then one may hope that COBOL will have been superseded.

Since CURRENT-DATE expects an alphanumeric item, whereas the other functions expect plain numeric items, you should define the data item used to get the current date as follows:

01 DATE-TODAY.
    05 DATE-TODAY-NUM PIC 9(8).
	

OR

01 DATE-TODAY PIC X(8).
01 DATE-TODAY-NUM REDEFINES DATE-TODAY PIC 9(8).
	

Then use DATE-TODAY with CURRENT-DATE and DATE-TODAY-NUM with everything else.

NB. In the following explanations, "numeric" always means PIC 9 DISPLAY.

CURRENT-DATE

Syntax

MOVE FUNCTION CURRENT-DATE TO data-item.

Action

Returns the current date in YYYYMMDD format. data-item must either be PIC X(8) or a group item.

INTEGER-OF-DATE

Syntax

COMPUTE dest = FUNCTION INTEGER-OF-DATE (source).

Action

Converts source into an integer representing the number of days between December 31, 1600 and source and stores it in dest. source must be a numeric data item containing a date formatted YYYYMMDD. dest must be a numeric data item of at least 6 digits.

DATE-OF-INTEGER

Syntax

COMPUTE dest = FUNCTION DATE-OF-INTEGER (source).

Action

Converts source into a date formatted YYYYMMDD and stores it in dest. source must be a numeric data item. dest must be a numeric data item of 8 digits.

INTEGER-OF-DAY

Syntax

COMPUTE dest = FUNCTION INTEGER-OF-DAY (source).

Action

Converts source into an integer representing the number of days between December 31, 1600 and source and stores it in dest. source must be a numeric data item containing a date formatted YYYYDDD; this is sometimes referred to as a date in Julian format. dest must be a numeric data item of at least 6 digits.

DAY-OF-INTEGER

Syntax

COMPUTE dest = FUNCTION DAY-OF-INTEGER (source).

Action

Converts source into a date formatted YYYYDDD and stores it in dest. source must be a numeric data item containing a positive integer. dest must be a numeric data item of 7 digits.