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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Calculate Age in years and days only!! 2

Status
Not open for further replies.

alexfromuno

Programmer
Nov 6, 2002
20
Hi All,

I have two date fields to work with:
Date of Transplant
system date
(Yes, this is a Hospital Transplant office)

What can I do to calcuate a persons time since transplant in years and days only?

i.e. the result will appear like:
21 y 264 d

I followed the thread marked in archive thread767-812520, but I'm just drawing a blank on what else to do.
I used the code there to calculate years, months and days ( result from above would be 21 year & 8 month & 24 days give or take), but that wasn't enough!!


Thanks for all help!
 
To get the years, @Years,
Code:
DateDiff("yyyy", {Transplant}, {sysdate})

Then find @ForDays,
Code:
DateAdd("yyyy", @Years, {Transplant})

Then @Days,
Code:
DateDiff("d", @ForDays, {sysdate})

Display as
Code:
@Years & " years and " & @Days & " days"
You can do extra testing and editing if you want to allow for values of 0 and 1.

I am assuming your {sysdate} may be different from the current date. If not, use Currentdate instead.



[yinyang] Madawc Williams (East Anglia, UK). Using Windows XP & Crystal 10 [yinyang]
 
Madawc,

You ROCK!!!!!

That solved the problem completely.

Thanks for alll.

alex
 
Madawc Williams,

One final question.

How would I make it print/display without the decimal?

ie...as 15 years & 230 days as oppossed
to 15.00 years & 230.00 days.

thanks again,

alex
 
totext({@years},0,"")+ " years and " + totext({@days},0,"") + " days"

-LB
 
You can also create a text field, type in the text portion and drop the formulas in, right click the formulas and use the usual format expert means to remove decimals.

-k
 
Madawc,
That datediff in years could be out by 1 year. All it does is subtract the two years in the fields.

What you really need is this to calculate the age accurately in years. The rest of your day stuff is fine, but the years will be out by one for the dates that haven't rolled over yet.

if month({Transplant}) < Month(CurrentDate) or
(month({Transplant}) = Month(CurrentDate) and
day({Transplant}) <= day(CurrentDate))
then year(CurrentDate)- Year({Transplant})
else year(CurrentDate)- Year({Transplant}) -1

Editor and Publisher of Crystal Clear
 
chelseatech, yes, you're right. @Years needs to be adjusted accordingly.

[yinyang] Madawc Williams (East Anglia, UK). Using Windows XP & Crystal 10 [yinyang]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top