INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
• Talk With Other Members
• Be Notified Of Responses
• Keyword Search
Favorite Forums
• Automated Signatures
• Best Of All, It's Free!

*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.

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

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

(OP)
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";

MthYrs;

--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.

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

### 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:

#### CODE

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
then

// 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)
then
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)
then
else
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.

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:

• Talk To Other Members
• Notification Of Responses To Questions
• Favorite Forums One Click Access
• Keyword Search Of All Posts, And More...

Register now while it's still free!