Technology Blogs by Members
Explore a vibrant mix of technical expertise, industry insights, and tech buzz in member blogs covering SAP products, technology, and events. Get in the mix!
cancel
Showing results for 
Search instead for 
Did you mean: 
Former Member

Hello All,

I came across a thread some days back where an end user wanted the Age of a customer in X Years Y Months and Z Days format given tow dates.

I searched through a lot of blogs but most of them didn't considered leap year which may lead to an incorrect data.

I tweaked the logic which I found in one of the blog and got that working so thought of sharing with all of you as we may come across such requirements in future.

So here how it goes. I will take two dates as Current Date([CurrDate]) and  Birth Date([BDate]).

First I will calculate the number of years in a formula which I have been told by an expert that breaking a huge formula into smaller pieces makes it more readable and easy to understand

[NumberOfYears]=Year ([CurrDate]) - Year ([BDate]) - If ( [CurrDate] <[BDate];1 ; 0 )+" Years "

Now I will calculate the number of months

[NumberOfMonths]=(Mod (((MonthNumberOfYear([CurrDate]) - MonthNumberOfYear([BDate])) + 12) - If ( DayNumberOfMonth([CurrDate]) < DayNumberOfMonth([BDate]) ; 1 ; 0 ); 12 ))+" Months "

And last the number of Days

[NumberOfDays]=(DayNumberOfYear([CurrDate]) - DayNumberOfYear( [BDate]) + If ( DayNumberOfYear([CurrDate])  >= DayNumberOfYear( [BDate]) ; 0 ; If ( DayNumberOfYear( [CurrDate]) - DayNumberOfYear([CurrDate])  < DayNumberOfYear( [BDate]) ; DayNumberOfYear( [BDate]) ; DayNumberOfYear( [CurrDate])- DayNumberOfYear( [CurrDate])  ) ))+" Days"

After we do all this only task left is to concatenate these three formula's.

[Age]=[NumberOfYears]+[NumberOfMonths]+[NumberOfDays]

and you will get the data in below format.

I have tested the code with Leap year Dates and some other random dates but your valuable feed backs and suggestions are always welcome.

Hope you like it.

Regards

Niraj

6 Comments
Labels in this area