Print Page | Close Window

formula to calculate age

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=16304
Printed Date: 05 May 2024 at 9:22pm


Topic: formula to calculate age
Posted By: sanchezgmc06
Subject: formula to calculate age
Date Posted: 27 Apr 2012 at 12:01pm
Hi i need a formula to calculate the clients age. I have the date of birth field
 
thanks



Replies:
Posted By: yggdrasil
Date Posted: 30 Apr 2012 at 3:31am
numbervar dys;
numbervar yrs;
days :=DateDiff ('d', date_of_birth,currentdate );
yrs := int(dys/365);
dys := dys - yrs * 365;
Age will be  'yrs' years  and 'dys' days. 
 
If you want age as decimal years just use yrs := dys/365
 
 


Posted By: DBlank
Date Posted: 30 Apr 2012 at 3:59am
another approach is to use the datediff and datepart together to avoid leap year issues...
 
datediff('yyyy',{table.DOB},currentdate)-(if datepart('y',currentdate)>datepart('y',{table.DOB}) then 0 else 1)


Posted By: aaml216
Date Posted: 30 Apr 2012 at 6:37am
DBlank - this is so helpful - I copied and pasted it right into my formula editor (changing table.field).  How do I get age in months? (babies)

-------------
Amanda from the Mid-Atlantic


Posted By: DBlank
Date Posted: 30 Apr 2012 at 6:51am
how are you defining months exactly?


Posted By: kostya1122
Date Posted: 30 Apr 2012 at 6:59am
this could work if the kids are under 1
(currentdate - {MEMB_COMPANY_V.BIRTH})/365*12


Posted By: aaml216
Date Posted: 30 Apr 2012 at 7:02am
kostya - I'll give that a try.  It looks like how I write formulas in Excel. 
 
DBlank - I am only looking at kids under 2 so I want to see:
--3 months old rather than 0 years old
--19 months old rather than 1 year old


-------------
Amanda from the Mid-Atlantic


Posted By: DBlank
Date Posted: 30 Apr 2012 at 7:09am
this does not look to see the days of the month but it would be sometning like this
 
if
//months
(datediff('yyyy',{@dob},currentdate)-(if datepart('y',currentdate)>datepart('y',{@dob}) then 0 else 1))<2 then
totext(datediff('m',{@dob},currentdate),0,'') & ' months'
else
//years
totext((datediff('yyyy',{@dob},currentdate)-(if datepart('y',currentdate)>datepart('y',{@dob}) then 0 else 1)),0,'') & ' years'


Posted By: sanchezgmc06
Date Posted: 30 Apr 2012 at 8:09am
perfect! thanks


Posted By: dbodell
Date Posted: 30 Apr 2012 at 9:59am
It's actually more accurate if you divide by 365.25 due to leap years.

-------------
Thanks,
D. Bodell



Print Page | Close Window