Calclulate age from DOB (date of birth)

Verified
Added by iNTERFACEWARE

This function calculates age from DOB, it returns years, months and partial years (i.e., 17, 3, 17.296272).

Source Code
require 'dateparse'
local age = require 'age'


function main(Data)
   local Msg = hl7.parse{vmd ='example\\demo.vmd', data = Data}
   
   -- using dateparse allows common date formats
   local AgeYr, AgeMth, AgeDec = age.getAge(Msg.PID[7][1]:D())
   trace(AgeYr, AgeMth, AgeDec)

   AgeYr, AgeMth, AgeDec = age.getAge('19980210')
   trace(AgeYr, AgeMth, AgeDec)

   AgeYr, AgeMth, AgeDec = age.getAge('1998-02-10')   
   trace(AgeYr, AgeMth, AgeDec)   
end
Description
This function calculates age from DOB, it returns years, months and partial years (i.e., 17, 3, 17.296272).
Usage Details

The code uses the age module to calculate age from DOB, the calculation allows for leap years.

Note: The age module uses dateparse so it accepts most common date formats.

How to use the code:

  • Paste it into a Filter or To Translator or load the attached project zip
    Note: If you paste the code you will need to add the age module
  • Load some sample HL7 messages, you can use the attached sample_data.txt
    Note: The project file already contains the sample messages
  • Look at the code and annotations to see how it works