Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

N months from a give yyyymmdd date? 1

Status
Not open for further replies.

Dan01

Programmer
Jun 14, 2001
439
US
Hi, does someone have an example on how to, say, get a yyyymmdd 9 months prior to 20010301? Thanks, Dan.
 
Please specify operating system and compiler. (I ahve looked at some of your previous posts, to no avail.) Some compilers are more advanced than others...
Tom Morrison
 
Thank Tom. Hewlett-Packard COBOL II, includes $POST85 for Cobol 85 uses. Dan.
 
Dan, it depends on your definition of 9 months. If 9 months equals 270 days, then use INTEGER-OF-DATE intrinsic function to get an integer value, subtract 270, and use DATE-OF-INTEGER on the result. (273 or 274 would be a closer estimate of 9 months.)

If 9 months means "same day of month, none months earlier" then one would subtract 9 from the month value, with appropriate borrow from the year value, and a fixup for day-of-month in the range 29-31.

So, what are the business rules?
Tom Morrison
 
It would be nine months earlier, same day of the month. Thanks, Dan.
 
Tom, if you have sample code, that would be great. Thanks in advance. Dan.
 
Okay, Dan. Here is a skeleton. I am leaving several things to you (such as leap year, making sure your date is valid to begin with, etc), but this is a start:

in DATA DIVISION:

Code:
01  DATE-TO-CHANGE.
    03  YYYY   PIC 9(4).
    03  MM     PIC 99.
        88  VALID-MONTH values 1 thru 12.
    03  DD     PIC 99.

01  VALUE "312831303130313130313031".
    03  DAYS-IN-MONTH  PIC 99 OCCURS 12.

in PROCEDURE DIVISION, after making sure that DATE-TO-CHANGE contains a valid numeric date (example for 9 months):

Code:
    IF MM < 10
        ADD 3 TO MM
        SUBTRACT 1 FROM YYYY
    ELSE
        SUBTRACT 9 FROM MM
    END-IF.
    IF DD > DAYS-IN-MONTH (MM)
        MOVE DAYS-IN-MONTH (MM) to DD
    END-IF.

Happy coding!

Tom Morrison
 
Thanks Tom, most kind of you. Dan.
 
I would simply include a query using the sysdummy1 table to get the date.

Select current date - 9 months
from sysibm.sysdummy1

Let DB2 or your sql deal with it....
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top