Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here


Crystal Reports 2008 Calculate Age in Years,Months, and days

Crystal Reports 2008 Calculate Age in Years,Months, and days

Crystal Reports 2008 Calculate Age in Years,Months, and days

Hello All,

I am having a difficult time calculating Age to display as years, months, and days.  so far, i have got the years and months down (so i think) and am having trouble with the days part.  Can anyone help?  This is the formula i am using for years and months taken from a previous post here on this site:

//Age in Years and Months
NumberVar DoBVar := IIF((100 * MONTH(CURRENTDATE) + DAY(CURRENTDATE)) < (100 * MONTH({person.birthdate}) + DAY({person.birthdate})), 1, 0);
NumberVar MthVar := (DATEDIFF("m",{person.birthdate},CURRENTDATE) - DobVar) MOD 12;
NumberVar YrsVar := DATEDIFF("yyyy",{person.birthdate},CURRENTDATE) - DobVar;
StringVar MthYrs := TOTEXT(YrsVar,0) + " years " + TOTEXT(MthVar,0) + " months";


--Any help would be greatly appreciated!! thanks, E


RE: Crystal Reports 2008 Calculate Age in Years,Months, and days

It isn't necessary to do it with variables.  Use some of Crystal's own commands, which include Remainder.

Also try displaying total days and the calculated years, months and days as a diagnostic till the results are what you expect.

yinyang Madawc Williams (East Anglia, UK).  Using Crystal 11.5 with SQL and Windows XP yinyang  

RE: Crystal Reports 2008 Calculate Age in Years,Months, and days

I have used the followinf formula with success. It is Ken Hamady's work, with date formulas
stringvar x := {PM_EMPMASTER.BIRTH_DATE};
datevar birth := date(val(left(x,4)),val(mid(x,5,2)),val(right(x,2)));
DateVar Ann := CurrentDate;  // Replace this with CurrentDate to get their age as of the time of the report
                                                            //or the date field of an event to get their age as of the time of that event.
if (Month(Ann) * 100) + Day (Ann) >=(Month(Birth) *100) + Day (Birth)  
then Year (Ann) - Year(Birth)  
else Year (Ann) - Year(Birth) -1

If not his, then not sure where I "stole" it from, but it ain't mine..

RE: Crystal Reports 2008 Calculate Age in Years,Months, and days

This is a little long, but it's a different approach that might be of use to you:


datevar v_lastBD;
numbervar v_years;
numbervar v_months;
numbervar v_days;

v_years := floor(datediff("m",{@DOB},currentdate)/12);

// Calculate most recent birthday.
v_lastBD := date(year(currentdate),month({@DOB}), day({@DOB}));

// Adjust for those who have not had a birthday in the current year.
if v_lastBD > currentdate
v_lastBD := date(dateadd("yyyy",-1,v_lastBD)) else v_lastBD;

// Calcluate months between today and most recent birthday.
v_months := datediff("m", v_lastBD,currentdate);

// Adjust for those whose birthday day of month has not occurred in the current month.
if day(v_lastBD) > day(currentdate)
v_months := v_months - 1 else v_months;

// Caculate days between the current date and the birthday day of month from the previous month.
if day({@DOB}) > day(currentdate)
v_lastBD := date(dateadd("m",-1,date(year(currentdate),month(currentdate),day(v_lastBd))))
v_lastBD := date(year(currentdate),month(currentdate),day(v_lastBd));

v_days := datediff("d",v_lastBD, currentdate);

//Change as needed to format display.
totext(v_years,0) + " years, " + totext(v_months,0) + " months, " + totext(v_days,0) + " days."


Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close