If you've ever needed to calculate the number of years, months and days between two dates, say to figure out someone's age. Here is a rule that will do just that.

The fields for Birthday and Today need to be defined as Datetime and be populated. I used the system variable on the Today field to get today's date.

The fields for Years, Months and Days need to be defined as integers and do not need to be populated. You can of course substitute variables for the fields and it'll work the same.

integer b_year, t_year, b_month, t_month, b_day, t_day;

b_year = 0;

t_year = 0;

b_month = 0;

t_month = 0;

b_day = 0;

t_day = 0;

b_year = get years(#Birthday);

t_year = get years(#Today);

b_month = get months(#Birthday);

t_month = get months(#Today);

b_day = get days(#Birthday);

t_day = get days(#Today);

//Calculate years

#Years = t_year - b_year;

//If the birthday is later in the year

//subtract a year

If b_month > t_month then

#Years = #Years - 1;

//If it's the same month but later in the month

//subtract a year

If (b_month = t_month) & (b_day > t_day) then

#Years = #Years - 1;

//Calculate months

#Months = t_month - b_month;

//If birthday is before today, need to add 12

If #Months < 0 then

#Months = #Months + 12;

//Calculate days

#Days = t_day - b_day;

//If birthday is after today, it'll be negative

//not sure how to handle it, but adding 30 will be close

//You could add the number of days in the birth month or today's month

If #Days < 0 then

Begin

#Days = #Days + 30;

If #Months = 0 then

#Months = 12;

#Months = #Months - 1;

End

There are a few other ways you could tweak the rule as well, you could use the julian date for the check to see if the Birthday is later in the year for example.

Depending what you need for the days, you could use an user exit to get the number of days in either the current month or birth month.

You can leave out the part for calculating months and days if you only need the years...

It all just depends on what exactly you need.

If anyone has a great ideas for the number of days, please leave a comment.

As always, any and all comments are welcome. Even a quick “thanks”, to let us know that you found this somewhat useful, is appreciated!

Feel free to request any topics or ideas as well.

Thanks for reading!

Pat Frey – IBM Support