Here is a custom function that you may call from rules or transformations that will return the age in your choice of years, months, days.
Note that this is a complete export.
<?xml version="1.0" encoding="UTF-8"?>
<Export generator="Cache" version="25" zv="Cache for Windows (x86-64) 2017.2.1 (Build 801U)" ts="2020-03-16 16:31:49">
<Class name="Custom.GetAge">
<Description>
Calculates and returns the age from the birthday passed in %Y%m%d format comparing to current date or passed in date.
ReturnUnits is "Y" (Default) for Years, "m" for Months, or "d" for Days
currentday in HL7 format, defaults to today if blank </Description>
<ProcedureBlock>1</ProcedureBlock>
<Super>Ens.Rule.FunctionSet</Super>
<TimeChanged>65454,59370.735262</TimeChanged>
<TimeCreated>62585,49624.168815</TimeCreated>
<UDLText name="T">
<Content><![CDATA[
// ClassMethod GetAge(birthday As %String, ReturnUnits As %String, currentday As %String) As %String [ Final ]
]]></Content>
</UDLText>
<Method name="GetAge">
<Description>
returns the current age in years with birtday passed in %Y%m%d format</Description>
<Final>1</Final>
<ClassMethod>1</ClassMethod>
<FormalSpec>birthday:%String,ReturnUnits:%String="Y",currentday:%String=""</FormalSpec>
<ReturnType>%String</ReturnType>
<Implementation><![CDATA[
// convert date times into correct format
set Tbirthformat = ##class(Ens.Util.Time).ConvertDateTime(birthday,"%Y%m%d","%q(3)" )
if currentday '= ""
{
set Tcurrentformat = ##class(Ens.Util.Time).ConvertDateTime(currentday,"%Y%m%d","%q(3)")
}
else
{ set Tcurrentformat = $h
}
set bmonth = $SYSTEM.SQL.DATEPART("mm",Tbirthformat)
set cmonth = $system.SQL.DATEPART("mm",Tcurrentformat)
set bday = $system.SQL.DATEPART("dd",Tbirthformat)
set cday = $system.SQL.DATEPART("dd",Tcurrentformat)
set tretval = 0
if (ReturnUnits = "Y") || (ReturnUnits = "y") || (ReturnUnits = "")
{
// calculate the difference in years by subtracting the birthday from the currentday
set difftimeYear = $SYSTEM.SQL.DATEDIFF("yy",Tbirthformat,Tcurrentformat) // get the years
if bmonth = cmonth //check day since the month is the same
{
if cday < bday
{
set difftimeYear = difftimeYear - 1
}
}
else
{ if cmonth < bmonth
{
set difftimeYear = difftimeYear - 1
}
}
// return the age in years
set tretval = difftimeYear
}
else
{ if (ReturnUnits = "M") || (ReturnUnits = "m")
{
set difftimeMonth = $SYSTEM.SQL.DATEDIFF("mm",Tbirthformat,Tcurrentformat) // get the months
if bmonth = cmonth
{ if cday < bday
{ set difftimeMonth = difftimeMonth - 1
}
}
// return the age in months
set tretval = difftimeMonth
}
else
{ if (ReturnUnits = "D") || (ReturnUnits = "d")
{ set difftimeDay = $SYSTEM.SQL.DATEDIFF("dd",Tbirthformat,Tcurrentformat) // get the days
// return the age in days
set tretval = difftimeDay
}
}
}
quit tretval
]]></Implementation>
</Method>
</Class>
</Export>
- Log in to post comments
.png)
.png)
.png)