Date Mathematics in ILE RPG IV | Add/Subtract Days, Months or Year From Date

%Days, %Months, %Years functions
We always need to add or subtract few months, days, years to a specific date or find the time-period in years, days or months. In this article we will learn this special mathematics.

First step to perform date mathematics:-
This step and the last step would not be required if we are dealing with an actual date, a date field which has been defined to be of date data type. However, in most of the cases, this would not be the practical case. We will get a date in numeric or character format. So, first step would be to convert these numeric and character program date fields into an actual date field. Did the format of this date come into your mind? Well, once a date acquires its own data type, date formats do not matter.

Converting a numeric date field into actual date data type is a one step process. To learn more about how to convert numeric date field into actual date data type, refer this article. Ok, once we have got a date field, we should begin with our actual basic mathematics.

  1. Add few days, months or year to current date.
  2. Subtract few days, months or year to specific date.
  3. Find out difference between two dates.

Last step to perform date mathematics:-
The last step in performing date mathematics is to get the date back into the original numeric format.

The concept is further clarified with the examples given below.


Example to add days, months or year to specific date in RPG IV
In RPG IV, we use built in functions %Years, %Months and %Days to convert a numeric literal into years, months and days and finally add it to any specific date as illustrated in the example given below.
DName+++++++++++ETDsFrom+++To/L+++IDc.Keywords+++++++
D W@YMD           S              6  0 Inz(000101)    
D W@Date          S               D                  
 * First step is to get an actual date               
CL0N01Factor1+++++++Opcode&ExtFactor2+++++++Result+++
C     *YMD          Move      W@YMD         W@Date   
 * Add 5 years                                       
C                   Eval      W@Date += %Years(5)    
 * Add 11 months                                     
C                   Eval      W@Date += %Months(11)  
 * Add 30 days                                       
C                   Eval      W@Date += %Days(30)    
C     *YMD          Move      W@Date        W@YMD    
C     W@YMD         Dsply                            
C                   Return                           

Output:-

DSPLY   51231

Example to subtract days, months or years from specific date in RPGLE IV
DName+++++++++++ETDsFrom+++To/L+++IDc.Keywords+++++++
D W@YMD           S              6  0 Inz(000101)    
D W@Date          S               D                  
D W@Date2         S              6  0                
 * First step is to get an actual date               
CL0N01Factor1+++++++Opcode&ExtFactor2+++++++Result+++
C     *YMD          Move      W@YMD         W@Date   
 * Sutract 5 years                                       
C                   Eval      W@Date -= %Years(5)    
 * Subtract 11 months                                     
C                   Eval      W@Date -= %Months(11)  
 * Subtract 30 days                                       
C                   Eval      W@Date -= %Days(30)    
C     *YMD          Move      W@Date        W@YMD    
C     W@YMD         Dsply                            
C                   Return                           

Output:-

DSPLY  940102