Difference of two dates in ILE RPG
To calculate difference between two dates, we use the built in function %Diff. This function can be used to get the difference in days, months or years as per our wish/requirement. One word of caution though; the remainder value is not returned as decimal parts.
So to calculate exact difference, this function should be used to get the difference in days and this difference should be divided by appropriate no like 365 or 365.25 to get exact difference year in decimal. A similar approach should be followed for months. Otherwise separate years, months and days difference should be calculated and should be clubbed together by some logic to suit our specific business requirement.
In this article we will learn to find the difference between two date fields in years, months and days respectively.
Just like any other date mathematics, here also, the first step is to get the two numeric dates into date data types. Once we have two valid dates of date data type we just have to get the difference using the %Diff built in functions.
The syntax of function %Diff is as below
%Diff(Date1: Date2: *Years/*Months/*Days)
Here Date1 and Date2 are two actual dates. And *Years, *Months and *Days are the difference of which are returned. i.e. *Years will return the difference of years.
The following example would make the ILE RPG date difference concept clearer.
DName+++++++++++ETDsFrom+++To/L+++IDc.Keywords+++++++++++++++++++++++++
D W@YMD1 S 6 0 Inz(011219)
D W@YMD2 S 6 0 Inz(000101)
D W@Date1 S D
D W@Date2 S D
D w@Yrs S 2 0 Inz
D w@Mons S 3 0 Inz
D w@Days S 5 0 Inz
*. 1 ...+... 2 ...+... 3 ...+... 4 ...+... 5 ...+... 6 ...+... 7 ...+.
* First step is to get actual dates
C *YMD Move W@YMD1 W@Date1
C *YMD Move W@YMD2 W@Date2
* Get difference of years
C Eval W@Yrs = %Diff(W@Date1: W@Date2: *Years)
* Get difference of months
C Eval W@Mons = %Diff(W@Date1: W@Date2: *Months)
* Get difference of days
C Eval W@Days = %Diff(W@Date1: W@Date2: *Days)
C W@Yrs Dsply
C W@Mons Dsply
C W@Days Dsply
C Return Output:-
DSPLY 1 DSPLY 23 DSPLY 718
As usual comments and feedbacks are most welcome.
Recent comments
5 days 1 hour ago
5 days 2 hours ago
5 days 2 hours ago
5 days 4 hours ago
5 days 15 hours ago
5 days 17 hours ago
5 days 18 hours ago
5 days 19 hours ago
6 days 8 hours ago
6 days 18 hours ago