Calculating Age

Any smart guy can suggest how to caluculate the age in a smart way?

I think of the follows but it has discrepancy when there are leap years between the birthday and today.

age = (today - birthday) / 365.
 

M-HT

Member
Try this:

years = YEAR(TODAY).
lastbirthday = DATE(MONTH(birthday), DAY(birthday), years) NO-ERROR.
IF ERROR-STATUS:ERROR THEN lastbirthday = DATE(2, 28, years).
IF lastbirthday > TODAY THEN DO:
lastbirthday = DATE(MONTH(birthday), DAY(birthday), years - 1) NO-ERROR.
IF ERROR-STATUS:ERROR THEN lastbirthday = DATE(2, 28, years - 1).
END.

years = YEAR(lastbirthday) - YEAR(birthday).
age = years + (TODAY - lastbirthday) / 365.

And if you want it to be really really exact then check if there is 29 february between today and lastbirthday and divide by 366 instead of 365.

Roman Pauer
 
finally I wrote this lines and I think should be perfect.


staff-age = YEAR(today) - YEAR(pol-staff.date-birth).

birth-this-year = DATE(STRING(MONTH(pol-staff.date-birth)) + "/"
+ STRING(DAY(pol-staff.date-birth)) + "/"
+ STRING(YEAR(TODAY))).

IF birth-this-year > TODAY THEN staff-age = staff-age - 1.
 

M-HT

Member
It's almost perfect, but if someone was born on 29 february and this year is not leap year it will not work.
See my previous post and take years instead of age as a result.
 
We do it like these routines: -

/********************************************************************
**
** File: date2age.p
**
** Author: Chris Paulson
**
** Date: 14-mar-95
**
** Calculates a age from a date
**
** Mods:-
** CJP - 13-Sep-95
** Reserve space for translating text
**
** M.Hills - 31/01/2000
** Changed "\" to "/" as causing problems with UNIX.
*/
define input param dt as date no-undo.

define var i as int no-undo.
i = today - dt.

run general/agetext.p( i ).

return return-value.
/* end proc */







/************************************************
**
** File: agetext.p
**
** Author: Chris Paulson
**
** Date: 14-jul-94
**
** Display an age in nice format
**
** Mods:-
** CJP - 13-Sep-95
** Reserve space for translating text
** Surjit - Added 0.5 if reminder is greater than 0.9
*/
define input param d as integer no-undo.
define var t as char format "x(60)":u no-undo.
define var vdiff as decimal no-undo.


if d >= 365.25 then do:

vdiff = d / 365.25.
vdiff = vdiff - integer(vdiff - 0.5).
if vdiff > 0.9 then
d = d + 0.5.

t = string( integer(truncate(d / 365.25,0))) + "Y":T4.
end.
else if d >= 169 then
t = string( integer(d / 30.4375)) + "M":T4.
else if d >= 30.4375 then
t = string( integer(d / 7.024038461538)) + "W":T4.
else
t = string( d ) + "D":T4.
return t.

/* end proc */





/********************************************************************
**
** File: calcage.p
**
** Author: Chris Paulson
**
** Date: 14-jul-94
**
** Calculates a inputed age into the number of days
**
** Mods:-
** CJP - 13-Jul-95
** Reserve space for translating text
*/
define input param age-inp as character no-undo.
define input param defUnit as char no-undo.
define output param age-int as integer no-undo.

define var tDec as decimal no-undo.

if (age-inp = "") or (age-inp = ?) then do:
age-int = ?.
return.
end.

if index(age-inp, "M":T4) > 0 then do:
age-int = decimal( substring(age-inp, 1, index(age-inp, "M":T4) - 1 ) ) * 30.4375 no-error.
end.
else if index(age-inp, "W":T4) > 0 then do:
age-int = decimal( substring(age-inp, 1, index(age-inp, "W":T4) - 1 ) ) * 7.024038461538 no-error.
end.
else if index(age-inp, "D":T4) > 0 then do:
age-int = decimal( substring(age-inp, 1, index(age-inp, "D":T4) - 1 ) ) no-error.
end.
else if index(age-inp, "Y":T4) > 0 then do:
assign tDec = decimal( substring(age-inp, 1, index(age-inp, "Y":T4) - 1 ) ) no-error.
if tDec > 0 then
age-int = 1 + tDec * 365.25 no-error.
else do:
if tDec < 0 then
assign age-int = -1. /* Any negative value in order to cause a bad return */
else
assign age-int = 0.
end.
end.
else do:
/*********CDW mod 7/1/97 ***********/

if defUnit = "Y":T4 then
age-int = 1 + decimal( age-inp ) * 365.25 no-error.
else if defUnit = "M":T4 then
age-int = decimal( age-inp ) * 30.4375 no-error.
else if defUnit = "W":T4 then
age-int = decimal( age-inp ) * 7.024038461538 no-error.
else if defUnit = "D":T4 then
age-int = decimal( age-inp ) no-error.
end.

if error-status:error or age-int < 0 then do:
message "Invalid format":25T skip "Please re-enter":20T skip.
return "bad":u.
end.

return "good":u.

/* end proc */
 
I am too careless to ommit the case of 29 Feb.

thx for all coding providers. I never think of calculating age could be so complicated.
 

Ofi

New Member
Easy DECISION

FUNCTION age RETURNS INT
( INPUT pdBirthDay AS DATE ).

RETURN YEAR(TODAY) -
YEAR(pdBirthDay) -
IF MONTH(TODAY) < MONTH(pdBirthDay) OR
MONTH(TODAY) = MONTH(pdBirthDay) AND DAY(TODAY) < DAY(pdBirthDay)
THEN 1
ELSE 0
.
END.

Function work with this logic:
If someone was born on 29 February hi will not have a birthday on 28 February regardless of year type!
 
U

Unregistered

Guest
oh yeah, it's a simple and precise function to return the age, great!!
 
Top