Logo

Programming-Idioms

  • Smalltalk
  • Cobol

Idiom #99 Format date YYYY-MM-DD

Assign to the string x the value of the fields (year, month, day) of the date d, in format YYYY-MM-DD.

IDENTIFICATION DIVISION.
PROGRAM-ID. date format.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 curr-date.
   03 yyyy      pic 9(4).
   03 mm        pic 9(2).
   03 dd        pic 9(2).
01 d.
   03 year      pic 9(4). 
   03 FILLER    pic x VALUE '-'.
   03 month     pic 99. 
   03 FILLER    pic x VALUE '-'.
   03 day       pic 99. 
PROCEDURE DIVISION.
   MOVE FUNCTION CURRENT-DATE TO curr-date
   MOVE yyyy to year
   MOVE mm   to month
   MOVE dd   to day
   DISPLAY d 
STOP RUN.
x := d yyyymmdd.
with Ada.Calendar.Formatting;
X : constant String :=
    Ada.Calendar.Formatting.Image (D) (1 .. 10);

The Image function returns time as well therefore the slice.

New implementation...