age.lua
Verified Featured
Added by iNTERFACEWARE
This module calculates age from DOB, it returns years, months and partial years (i.e., 17, 3, 17.296272)
Source Code
require 'dateparse' local age = {} local function isLeapYr(Year) if math.fmod(Year/400,1) == 0 then return true end if math.fmod(Year/4,1) == 0 and math.fmod(Year/100,1) ~= 0 then return true end return false end local function calcYrs(DOB) local _, dob = dateparse.parse(DOB) local T = os.time() local ageSecs = os.difftime(T, os.time{ year = dob.year, month = dob.month, day = dob.day}) local currY = tonumber(os.ts.date('%Y')) local yrs = 0 local YRSEC = 365*24*3600 local LPYRSEC = 366*24*3600 local secs = ageSecs for i = dob.year, currY do if isLeapYr(i) then secs = secs - LPYRSEC yrs = yrs + 1 if secs < YRSEC then return yrs, yrs + secs/YRSEC, secs end else secs = secs - YRSEC yrs = yrs + 1 if isLeapYr(1 + 1) then if secs < LPYRSEC then return yrs, secs/LPYRSEC, secs end else if secs < YRSEC then return yrs, yrs + secs/YRSEC, secs end end end end end local function calcMths(DOB) local _, dob = dateparse.parse(DOB) local _, currDt = dateparse.parse(os.date()) local ageMths if currDt.month > dob.month then ageMths = currDt.month - dob.month trace(ageMths) elseif currDt.month == dob.month then ageMths = 0 trace(ageMths) else ageMths = 12 + (currDt.month - dob.month) end return ageMths end function age.getAge(DOB) -- calculate years local ageYrs, ageDec = calcYrs(DOB) -- calculate months local ageMths = calcMths(DOB) return ageYrs, ageMths, ageDec end local HELP_DEF=[[{ "Desc": "Calculate age from date of birth (DOB). Returns years, months and partial years.", "Returns": [{ "Desc": "Years <u>integer</u>" }, { "Desc": "Months <u>integer</u>" }, { "Desc": "Partial years <u>number</u>" } ], "SummaryLine": "Calculate age from date of birth", "SeeAlso": [ { "Title": "age.lua - in our code repository.", "Link": "http://help.interfaceware.com/code/details/age-lua" } ], "Title": "age.getAge", "Usage": "local Years, Months, PartialYears = age.getAge{DOB}", "Parameters": [ { "DOB": { "Desc": "Date of birth <u>string</u>. " } } ], "Examples": [ "<pre>local Years, Months, PartialYears = age.getAge(DOB)</pre>" ], "ParameterTable": false }]] help.set{input_function=age.getAge, help_data=json.parse{data=HELP_DEF}} return age
Description
This module calculates age from DOB, it returns years, months and partial years (i.e., 17, 3, 17.296272)
Usage Details
This module calculates age from DOB (date of birth), the calculation allows for leap years. To calculate age use the age.getAge()
function which returns years, months and partial years (i.e., 17, 3, 17.296272).
Note: The age module uses dateparse so it accepts most common date formats.
How to use the code:
- Add the module to a Filter or To Translator
- Use the
age.getAge()
function to return the age from DOB
Here is some sample code for main()
:
-- This example shows the use of the age module -- This module calculates age from date of birth - it returns years, months and partial years. -- https://help.interfaceware.com/code/details/age-lua local age = require 'age' function main(Data) local Msg = hl7.parse{vmd ='example\\demo.vmd', data=Data} -- getAge() requires date as a string parameter AgeYr, AgeMth, AgeDec = age.getAge('19980210') trace(AgeYr, AgeMth, AgeDec) AgeYr, AgeMth, AgeDec = age.getAge('1998-02-10') trace(AgeYr, AgeMth, AgeDec) -- use :nodeValue() as getAge() requires date as a string local AgeYr, AgeMth, AgeDec = age.getAge(Msg.PID[7][1]:nodeValue()) trace(AgeYr, AgeMth, AgeDec) -- age.lua uses dateparse to supports non-conformant date formats local AgeYr, AgeMth, AgeDec = age.getAge('01/10/1948') trace(AgeYr, AgeMth, AgeDec) end
More Information