MainframeSupports
tip week 6/2018:

One of the hard things in any progamming language is the calculation of new dates from a known date. As far as I remember COBOL and PLI have given up on this a long time ago. If you are able to reach DB2 there are plenty of possibilities, but they all require a call to DB2. Recently I needed to do some date calculations in REXX. I have used it before to calculate week numbers. I must admit, though, that I have neglected to describe the date calculation part and I think it deserves more attention.

The central function for dates in REXX is of course DATE. And when it comes to date calculations you need to use the parameter 'B' for Base date like in DATE('B'). This expression will return a numeric value representing the number of days passed since the first of January year 1 until today. The expression DATE('B', '20170101', 'S') will return the number of days until the first of January 2017. Thus DATE('B') - DATE('B', '20170101', 'S') will return the number of days passed today since the first of January 2017.

Do you want to calculate which date it is in seven days (a week) in the format YYYYMMDD you can use the expression DATE('S', DATE('B') + 7, 'B'). A little more complicated, but not much is the date 28 days before the 15th of May 2017. Here you can use the expression DATE('S', DATE('B', '20170515', 'S') - 28, 'B'). Unfortunately there is no simple expression to get you exactly one month or one year forward or back. To do that you will have to use DB2 if you do not want to code it yourself.

Previous tip in english        Forrige danske tip        Tip list