TSO-ISPF JCL COBOL VSAM DB2 CICS Tools Articles Job Portal Forum Quiz Interview Q&A

COBOL - Date Function


The most useful intrinsic function is CURRENT-DATE which is a replacement for ACCEPT DATE and ACCEPT TIME. CURRENT-DATE is Y2K-compliant, having a 4-digit year. This function returns a 20-character alphanumeric field which is laid out as follows.

01  WS-CURRENT-DATE-FIELDS.
    05  WS-CURRENT-DATE.
        10  WS-CURRENT-YEAR    PIC  9(4).
        10  WS-CURRENT-MONTH   PIC  9(2).
        10  WS-CURRENT-DAY     PIC  9(2).
    05  WS-CURRENT-TIME.
        10  WS-CURRENT-HOUR    PIC  9(2).
        10  WS-CURRENT-MINUTE  PIC  9(2).
        10  WS-CURRENT-SECOND  PIC  9(2).
        10  WS-CURRENT-MS      PIC  9(2).
    05  WS-DIFF-FROM-GMT       PIC S9(4).

So not only can you get the time down to the millisecond, but you can get the difference between your time and Greenwich Mean Time.

The function is used in a MOVE.

MOVE FUNCTION CURRENT-DATE TO WS-CURRENT-DATE-FIELDS

COBOL Date Example:

You need to know what date is 150 days from today (and this kind of stuff happens more often than you’d think)? Convert today to an integer date, add 150 to it and convert it back. No more checking which months you’re going through to see if it’s a 30 day or 31 day month. No more leap year calculations.

01  WS-TODAY         PIC 9(8).
01  WS-FUTURE-DATE   PIC 9(8).
....
MOVE FUNCTION CURRENT-DATE (1:8) TO WS-TODAY.
COMPUTE WS-FUTURE-DATE = FUNCTION INTEGER-OF-DATE (WS-TODAY) + 150

Convert from Gregorian to Integer formats:

COMPUTE WS-INTEGER-DATE = FUNCTION INTEGER-OF-DATE (WS-DATE)

Convert from Integer to Gregorian formats:

COMPUTE WS-DATE = FUNCTION DATE-OF-INTEGER (WS-INT-DATE)

Convert from Julian to Integer formats:

COMPUTE WS-INTEGER-DATE = FUNCTION INTEGER-OF-DAY (WS-JUL-DATE)

Convert from Integer to Julian formats:

COMPUTE WS-JULIAN-DATE = FUNCTION DAY-OF-INTEGER (WS-INT-DATE)

  • All Gregorian and Julian dates are expected to have 4-digit years.
  • COMPUTE is OK because we’re only using integers here.

How many days between two dates?

COMPUTE WS-DAYS = FUNCTION INTEGER-OF-DATE (WS-DATE-1) - FUNCTION INTEGER-OF-DATE (WS-DATE-2)

Converting between Gregorian and Julian formats used to be a pain also. Now it’s easy as below.:

COMPUTE WS-DATE = FUNCTION DATE-OF-INTEGER (FUNCTION INTEGER-OF-DAY (WS-JULIAN))

Let us see COBOL examples - Example 1, Example 2

If you have any doubts or queries related to this chapter, get them clarified from our Mainframe experts on ibmmainframer Community!

Are you looking for Job Change? Job Portal