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!

Find Age based on DOB field 4

Status
Not open for further replies.

jenschmidt

Programmer
Sep 30, 2002
145
0
0
US
Good afternoon! I have a Date of Birth (date-time) field and I would like to calculate what the person's current age is. We tried {Table.DOB}- CurrentDate and it returned what I think is the number of days old the person is. Any other suggestions?

Thanks as always!
Jen

jennifer.giemza@uwmf.wisc.edu
 
Use Date Diff and use the year interval

DateDiff("y",{DOBField},CurrentDate)
 
Use the datediff function and specify the
interval as "yyyy".

DateDiff("yyyy", CurrentDate(), {Table.DOB})
 
I messed up the dates. It's start then End date. Specifying "y" is the same as "d" which gives the number of days different between the two dates.

DateDiff("yyyy", {Table.DOB}, CurentDate)
 
I noticed when you specify either "y" or "d" it gives you the number of days and "yyyy" gives you the number of years just by subtracting the year portion of the date field. Is there any function that takes into account the whole date field to return the correct age? If yesterday was someone's birthday, I think it would still show the same age today because the year is the same.

Thanks for all the great input! I appreciate the help!

jennifer.giemza@uwmf.wisc.edu
 
there's no function that I know of.

This is the only way I can think to do it:

numbervar yearDOBVal := year({DOBField});
numbervar monthDOBVal := month({DOBField});
numbervar dayDOBVal := day({DOBField});
numbervar yearCrntVal := year(CurrentDate());
numbervar monthCrntVal := month(CurrentDate());
numbervar dayCrntVal := day(CurrentDate());

numbervar finalYear := yearCrntVal - yearDOBVal;
numbervar finalMonth := monthCrntVal - monthDOBVal;
numbervar finalDays := dayCrntVal - dayDOBVal;

if finalDays < 0 then
finalMonth := finalMonth -1;
finalDays := monthCrntVal - finalDays;
end if

if finalMonth < 0 then
finalYear := finalYear -1;
finalMonth := 12 + finalMonth; //PLUS because the finalMonth is negative here
end if

&quot;Your age is &quot; & cstr(finalYear) & &quot; years, &quot; & cstr(finalMonth) & &quot; months and &quot; & cstr(finalDays) & &quot; days.&quot;



I haven't tried this before so I'm not sure about it, but on paper it seems to work fine.
 
This isn't correct, the datediff() using y tells the difference between the year of their brith and the current year, which will be wrong until both the month and the day have been reached.

If I'm born in December 31, 2002, and you check my age January 1, 2003, I'm now a year old, potentially prematurely denying me my Mums milk.

Your original try of using dob-currentdate is more accurate, but it might still be wrong because of Leap Year.

Try something like:

(If Month({table.dob}) < Month(CurrentDate) or
Month({table.dob}) = Month(CurrentDate))
and
Day({table.dob}) <= Day(CurrentDate)) then
Year(CurrentDate) - Year({table.dob})
else
Year(CurrentDate) - Year({table.dob})) – 1

-k
 
Ooops, should be:

If Month({table.dob}) <= Month(CurrentDate)
and
Day({table.dob}) <= Day(CurrentDate)) then
Year(CurrentDate) - Year({table.dob})
else
Year(CurrentDate) - Year({table.dob})) – 1
 
Here is the formula I use:

WhileReadingRecords;
DateVar Birth:= {DOBField};
DateVar Ann:=CurrentDate;

if (Month(Ann) * 100) + Day (Ann) >=(Month(Birth) *100) + Day (Birth)
then Year (Ann) - Year(Birth)
else Year (Ann) - Year(Birth) -1


Software Sales, Training, Implementation and Support for Exact Macola, eSynergy, and Crystal Reports
dgilsdorf@trianglepartners.com
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top