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

COBOL - Date Function Example 2


How to get the Current Date/Time in user defined format?

IDENTIFICATION DIVISION.
PROGRAM-ID. COBLDATE.

DATA DIVISION.
WORKING-STORAGE SECTION.
01 WS-DATETIME              PIC X(21).
01 WS-FORMATTED-DT.
   05 WS-FORMATTED-DTE-TME.
      15 WS-FORMATTED-YEAR  PIC  9(4).
      15 FILLER             PIC X VALUE '-'.
      15 WS-FORMATTED-MONTH PIC  9(2).
      15 FILLER             PIC X VALUE '-'.
      15 WS-FORMATTED-DY    PIC  9(2).
      15 FILLER             PIC X VALUE '-'.
      15 WS-FORMATTED-HOUR  PIC  9(2).
      15 FILLER             PIC X VALUE ':'.
      15 WS-FORMATTED-MINS  PIC  9(2).
      15 FILLER             PIC X VALUE ':'.
      15 WS-FORMATTED-SEC   PIC  9(2).
      15 FILLER             PIC X VALUE ':'.
      15 WS-FORMATTED-MS    PIC  9(2).

PROCEDURE DIVISION.
    MOVE FUNCTION CURRENT-DATE TO WS-DATETIME.

    MOVE WS-DATETIME(1:4)  TO WS-FORMATTED-YEAR.
    MOVE WS-DATETIME(5:2)  TO WS-FORMATTED-MONTH.
    MOVE WS-DATETIME(7:2)  TO WS-FORMATTED-DY.
    MOVE WS-DATETIME(9:2)  TO WS-FORMATTED-HOUR.
    MOVE WS-DATETIME(11:2) TO WS-FORMATTED-MINS.
    MOVE WS-DATETIME(13:2) TO WS-FORMATTED-SEC.
    MOVE WS-DATETIME(15:2) TO WS-FORMATTED-MS.

    DISPLAY WS-DATETIME.
    DISPLAY WS-FORMATTED-DT.
    STOP RUN.

When you compile and execute the above program, it produces the following result −

2018011005202041+0000
2018-01-10-05:20:20:41


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