Display formatted date in COBOL program

susmita  picture susmita · Nov 27, 2012 · Viewed 8.9k times · Source

I am taking a date variable in database in YYYY-MM-DD:hh:min:ss format and i want to display it in report YYYY-MM-DD HH:MIN:SS using cobol program.How to do this?

Answer

Brian Tiffin picture Brian Tiffin · Nov 30, 2012

Take a look at Intrinsic Functions, for example

  • FUNCTION COMBINED-DATETIME
  • FUNCTION CURRENT-DATE
  • FUNCTION DATE-OF-INTEGER
  • FUNCTION DATE-TO-YYYYMMDD
  • FUNCTION INTEGER-OF-DATE
  • FUNCTION LOCALE-DATE
  • There are a lot of others, google for the OpenCOBOL FAQ and see section 4.2 for examples

http://opencobol.add1tocobol.com/#does-opencobol-implement-any-intrinsic-functions

To get the current clock, look to ACCEPT FROM DATE YYYYMMDD, ACCEPT FROM TIME

Then you may want to create a PICTURE data clause, giving you absolute control over how your dates and times are formatted and displayed.

I've also found INSPECT REPLACING to be a handy thing when you want to quickly convert colons to spaces, or slashes etc.

    identification division.
    program-id. inspecting.

    data division.
    working-storage section.
    01  ORIGINAL            pic XXXX/XX/XXBXX/XX/XXXXXXX/XX.
    01  DATEREC             pic XXXX/XX/XXBXX/XX/XXXXXXX/XX.

    procedure division.

    move function when-compiled to DATEREC ORIGINAL

    INSPECT DATEREC REPLACING ALL "/" BY ":" AFTER INITIAL SPACE

    display
        "Intrinsic function WHEN-COMPILED " ORIGINAL
    end-display
    display
        " after INSPECT REPLACING         " DATEREC
    end-display

    goback.
    end program inspecting.

giving

Intrinsic function WHEN-COMPILED 2010/03/25 23/05/0900-04/00
 after INSPECT REPLACING         2010/03/25 23:05:0900-04:00