HANDLING DATES IN COBOL
The subject of handling dates is vast enough to consume a multi-day seminar or a
reference book. Although I'll be adding some information to this area it not intended to
be an exhaustive reference on dates. The purpose of this page is to give BRIEF answers to
questions that still linger in your mind after reading the text and hearing the class
lecture.
Method 1 (DAY)
You previously had the following line in the procedure division of your program:
ACCEPT DATE-P FROM DAY
DATE-P in this case represents a 5 byte field in your report heading. DAY returned the
Julian date from the system and placed it in this DATE-P field. The Julian date is in the
form YYDDD where YY is the year and DDD is the days number out of 365. Thus you saw
something like 99275 in your report heading line. 99001 would be January 1, 1999, etc.
01 HEADING-1.
05 PIC X(05) VALUE SPACES.
05 PIC X(15)
VALUE 'MY
REPORT TITLE'.
05 PIC X(05) VALUE SPACES.
05 DATE-P PIC X(5).
05 PIC X(3) VALUE SPACES.
05 PIC X(4) VALUE 'PAGE'.
05 PIC X(2) VALUE SPACES.
05 PAGE-NUMBER PIC ZZ9.
Method 2 (DATE)
In method 1 as in this method you will use the accept verb. The syntax is as follows:
ACCEPT IDENTIFIER FROM RESERVED WORD
The identifier specifies where the information will be placed. The reserved word (of
which their are many) in this case will be DATE. See your text for more reserved words
that can be used with accept to return the date in different forms. DATE returns the date
in the form YYMMDD. The line in the procedure division would be as follows:
ACCEPT IDENTIFIER FROM DATE
We haven't decided on an identifier yet. We want the date to be in the form MM/DD/YY.
This presents two problems. First, the system doesn't return the date in that order. The
system returns the year, the month and the day and we want the month, the day and the
year. Secondly, we want to add slash marks to improve readability in our heading. How do
we solve this problem?
Instead of accepting the DAY to the DATE-P field which is already in the heading, we
will accept the DATE into an intermediate field with the year month and day as elementary
items, and then move those elementary items into elementary items separated by slash marks
in the report heading. Consider the following area we will create in working storage:
**********THIS IS MY DATE STORAGE AREA*******
01 WS-DATE.
05 RUN-YEAR PIC 99.
05 RUN-MONTH PIC 99.
05 RUN-DAY PIC 99.
Notice I added a comment to separate and document this area. This is NOT your report
heading. This is a separate section of code you drop into a convenient area of working
storage. You may want to put it after your flags for instance. I called the group name
WS-DATE but you can choose anything that you feel is descriptive and helps self-document
the code. With that group name chose we would have a final ACCEPT statement as follows:
ACCEPT WS-DATE FROM DATE
Now when the above line of code runs in your procedure division you will have the year
in RUN-YEAR, the month in RUN-MONTH, and the day in RUN-DAY. We still haven't shifted the
date elements or inserted the slash marks. We will do this by the arrangement of
elementary items in the heading line. Next we modify the heading to include NOT DATE-P but
several new elementary items. We have added MM-OUT, DD-OUT, and YY-OUT as well as slashes
between them
01 HEADING-1.
05 PIC X(10) VALUE SPACES.
05 PIC X(12)
VALUE 'REPORT TITLE'.
05 PIC X(05) VALUE SPACES.
05 MM-OUT PIC XX.
05 PIC X VALUE "/".
05 DD-OUT PIC XX.
05 PIC X VALUE "/".
05 YY-OUT PIC XX.
05 PIC X(5) VALUE SPACES.
05 PIC X(4) VALUE 'PAGE'.
05 PIC X(1) VALUE SPACES.
05 PAGE-NUMBER PIC ZZ9.
05 PIC X(02) VALUE SPACES.
We finally add three more lines after the ACCEPT so the final code in the procedure
division dealing with the date is as follows:
ACCEPT WS-DATE FROM DATE
MOVE RUN-MONTH TO MM-OUT
MOVE RUN-DAY TO DD-OUT
MOVE RUN-YEAR TO YY-OUT
Our trick was really to accept the date into 3 elementary items instead of one large 6
byte field. Then we were free to move the elements into their respective fields in the
report heading line. We simply added a slash character in between the date elements in the
output by using a value of "/" instead of spaces.
CONCLUSION
I have only presented one of many variations you can use to accomplish the task of
putting MM/DD/YY in your heading line. Feel free to experiment with other methods.
You can also use a subprogram to handle your date and thus move most of the date code out
of the main program. This is a preferred method using code similar to
ldate.cbl listed under lecture notes on this website.
HOME