Print Page | Close Window

Age calculation in Yrs, Mos and Days

Printed From: Crystal Reports Book
Category: Crystal Reports 9 through 2020
Forum Name: Technical Questions
Forum Discription: Formulas, charting data, Crystal syntax, etc.
URL: http://www.crystalreportsbook.com/forum/forum_posts.asp?TID=21697
Printed Date: 19 Apr 2024 at 2:36am


Topic: Age calculation in Yrs, Mos and Days
Posted By: crystalsonic
Subject: Age calculation in Yrs, Mos and Days
Date Posted: 24 Sep 2015 at 11:03am
I need to calculate the age in years, months and days between DOB and a another date.
 
I've tried using the dateserial, datepart and datediff functions with no luck.
 
Any ideas?



Replies:
Posted By: crystalsonic
Date Posted: 24 Sep 2015 at 11:17am
I forgot to include my formula. I have some adjustments to make since some of the calculations are not accurate. Pretty close though...
 
If IsNull({Patients.DOB}) Then 'DOB Not Given' else
ToText(int(DateDiff('d',DateTimeValue({Patients.DOB}),{Episodes.DateColl})/365.25),0)+' Years '+
ToText(int(remainder(DateDiff('d',DateTimeValue({Patients.DOB}),{Episodes.DateColl}),365.25)/30),0)+' Months '+
If day({Episodes.DateColl}) < Day({Patients.DOB}) Then
ToText(day(dateserial(year({Patients.DOB}), Month({Patients.DOB})+1,1-1)) - Day({Patients.DOB}) + Day({Episodes.DateColl}),0) + ' days'


Posted By: kevlray
Date Posted: 24 Sep 2015 at 12:56pm
Ken Hamady has this formula on his web site.  I suspect it will work.  Go to  http://www.kenhamady.com/formulas/form06.shtml for more information.

WhileReadingRecords;
DateVar Birth:= {ages.Birth};   // Replace this with your field for Date Of Birth
DateVar Ann := {ages.DateAnn};  // Replace this with CurrentDate to get their age at runtime
                                                            //or a date field to get their age as of that date

//To increment the age of 'leap-year' babies on 2/28 instead of 3/1 (in non-leap years)

//if (Month(Ann) * 100) + Day (Ann) = 228
//and (Month(Ann+1) * 100) + Day (Ann+1) = 301
//and (Month(Birth) *100) + Day (Birth) =229
//then Year (Ann) - Year(Birth)
//else

if (Month(Ann) * 100) + Day (Ann) >=(Month(Birth) *100) + Day (Birth)
then Year (Ann) - Year(Birth)
else Year (Ann) - Year(Birth) -1



Posted By: crystalsonic
Date Posted: 25 Sep 2015 at 8:12am
I have used he Ken Hamady formula. However, the age is calculated in yrs vs. yrs, mos and days.



Print Page | Close Window