typestar

CURRENT-DATE and slicing in COBOL

One function call returns the whole timestamp; reference modification slices it.

IDENTIFICATION DIVISION.
PROGRAM-ID. CURRENT-DATE-DEMO.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 NOW-STAMP  PIC X(21).
PROCEDURE DIVISION.
*> CURRENT-DATE returns 21 characters; (start:length) slices them.
    MOVE FUNCTION CURRENT-DATE TO NOW-STAMP
    DISPLAY "Year:  " NOW-STAMP(1:4)
    DISPLAY "Month: " NOW-STAMP(5:2)
    DISPLAY "Clock: " NOW-STAMP(9:2) ":" NOW-STAMP(11:2)
    STOP RUN.

How it works

  1. FUNCTION CURRENT-DATE yields 21 characters: date, time, and zone offset.
  2. NOW-STAMP(1:4) is reference modification — start position, then length.
  3. Three slices pull the year, the month, and the clock out of one field.

Keywords and builtins used here

The run, in numbers

Lines
12
Characters to type
376
Tokens
60
Three-star pace
40 tpm

At the three-star pace of 40 tokens a minute, this run takes about 90 seconds.

Type this snippet

Step 2 of 3 in Functions & calls, step 23 of 27 in Language basics.

← Previous Next →