Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

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

Status
Not open for further replies.

ELMO1114

Programmer
Aug 31, 2011
1
0
0
US
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

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

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 
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) 
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
v_lastBD := date(dateadd("m",-1,date(year(currentdate),month(currentdate),day(v_lastBd))))
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."

 
Please everyone note that this thread should not have appeared in this forum, but instead belongs in forum767 or forum149.

-LB
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top