Calculate the age of a person
Hi all,
I've created a method to calculate the age of a person, or the age of a process, contract or other stuff as you want.
/// Calculate the age from date of birth and other date (by default shoud be today).
/// <b>dateOfBird</b> Date of birth in cache format. ie. $ZDH("1972-01-01",3)
/// <b>day</b> Date to calculate to. ie: $H
ClassMethod AgeAt(dateOfBirth, day) As %Integer{
quit:dateOfBirth=""
quit:day=""
set yeardob=$SYSTEM.SQL.DATEPART("year",dateOfBirth)
set yearday=$SYSTEM.SQL.DATEPART("year",day)
set years=yearday-yeardob
kill yeardob, yearday
set monthdob=$SYSTEM.SQL.DATEPART("month",dateOfBirth)
set monthday=$SYSTEM.SQL.DATEPART("month",day)
if (monthday-monthdob)<0 set years=years-1
if (monthdob-monthday)=0
{
if ($SYSTEM.SQL.DATEPART("day",day) - $SYSTEM.SQL.DATEPART("day",dateOfBirth)) < 0
{
set years=years-1
}
}
kill monthdob, monthday
quit years
}
I hope this method is useful for you
Best regards
[EDITED] Thanks @Marcus Magina for improve this method by a easy way.
Discussion (2)1
Comments
/// Calculate age by passing date of birth in internal format ( ex.: $ZDH("19/04/1993",4) )
ClassMethod currentAge(date As %Date = "") As %Integer [ CodeMode = expression ]
{
$select(date="":"",1:($ZD($H,8)-$ZD(date,8)\10000))
}
Thanks for your easy solution. I'll change my code to improve this.