Menu

COBOL - Exemplo de função de data 2


Como obter a data/hora atual no formato definido pelo usuário?

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.

Quando você compila e executa o programa acima, ele produz o seguinte resultado -

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