• Iguana 6
  • Previous Versions
  • API
  • Sample Code
  • Training
  • Create a Ticket
iNTERFACEWARE Help Center
  • Iguana 6
  • Previous Versions
  • API
  • Sample Code
  • Training
  • Create a Ticket

Code Repository

Home›Code Repository›age.lua
Modules

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
• Find age from date of birth
• Calclulate age from DOB (date of birth)
Bookmark
  • Reviews
  • Related Listings
Filter
Sort by: Newest First
  • Oldest First
  • Rating
  • Helpfulness
Write a Review
Rating
Keyword
Filter
Sort by: Title
  • Newest First
  • Oldest First
  • Most Reviews
  • Highest Rated
Rating
iNTERFACEWARE
auth.lua
Added by iNTERFACEWARE
Modules
A module that does basic authentication for incoming web requests
batch.lua
Added by iNTERFACEWARE
Modules
A module to help processing batched HL7 messages
codemap.lua
Added by iNTERFACEWARE
Modules
This module is used to map one set of codes to another set of codes, or to validate code membership in a set
csv_parse.lua
Added by iNTERFACEWARE
Modules
A module for parsing well-formed CSV files.
custom_merge.lua
Added by iNTERFACEWARE
Modules
A customizable database merge method for Iguana 5.5.1 and up.
dateparse.lua
Added by iNTERFACEWARE
Modules
A fuzzy date/time parser that is very useful for automatically translating a wide variety of date/time formats.
dup.lua
Added by iNTERFACEWARE
Modules
Duplicate message filter.
edifact.lua
Added by iNTERFACEWARE
Modules
Convert EDI messages to HL7 format so you can process them like an HL7 message (and convert them back to EDI afterwards)
hl7.findSegment.lua
Added by iNTERFACEWARE
Modules
A utility for finding any HL7 segment in a parsed HL7 message node tree.
hl7.serialize.lua
Added by iNTERFACEWARE
Modules
Serializes an HL7 message using specified non-standard delimiters and/or escape characters
hl7.zsegment.lua
Added by iNTERFACEWARE
Modules
Generic Z segment parser. Parses Z segments without needing grammar definitions in the VMD file.
iguanaServer.lua
Added by iNTERFACEWARE
Modules
Provides programmatic access to various operations that can be performed on Iguana channels.
llp.lua
Added by iNTERFACEWARE
Modules
Allows you to use LLP connections from a Translator script
mime.lua
Added by iNTERFACEWARE
Modules
Sends MIME-encoded email attachments using the SMTP protocol. A wrapper around net.smtp.send.
resubmit.lua
Added by iNTERFACEWARE
Modules
Resubmit a logged message to an Iguana channel using the unique reference number (refmsgid).
retry.lua
Added by iNTERFACEWARE
Modules
A module for retrying operations which might periodically fail like database operations.
rtf.lua
Added by iNTERFACEWARE
Modules
A module for converting a RTF file to plain text.
scheduler.lua
Added by iNTERFACEWARE
Modules
Schedule jobs to run at a specified time of day, very useful for batch processing
scrub.lua
Added by iNTERFACEWARE
Modules
The “scrub” module given below redacts sensitive information from HL7 messages.
sha1.lua
Added by iNTERFACEWARE
Modules
A pure Lua-based implementation of the popular SHA-1 hashing function.
Showing 1 - 20 of 31 results
«12»

Topics

  • expandGetting Started
  • expandAdministration
    • expandInstallation
    • expandLicensing
    • expandUpgrades
    • expandDeployment
    • expandConfiguration Management
      • expandCustom Configuration
    • expandBackup and Restore
    • expandSecurity
      • expandHIPAA Compliance
    • expandTroubleshooting
  • expandDeveloping Interfaces
    • expandArchitecture
    • expandInterfaces
      • expandHL7
      • expandDatabase
        • expandConnect
      • expandWeb Services
      • expandCDA
      • expandX12
      • expandOther Interfaces
      • expandUtilities
    • expandRepositories
      • expandBuiltin Repositories
        • expandIguana Upgrade
        • expandIguana Tutorials
        • expandIguana Tools
        • expandIguana Protocols
        • expandIguana Files
        • expandIguana Date/Time
        • expandIguana Webservices
        • expandIguana Excel
      • expandRemote Repositories
      • expandCS Team Repositories
        • expandIguana Channels
    • expandSample Code
      • expandModules
      • expandUsing built-in functions
      • expandWorking with XML
    • expandLua Programming
    • expandPerformance
  • expandFAQs and TIPs
    • expandFrequently Asked Questions
      • expandInstalls and Upgrades
      • expandWeb Services
      • expandConfiguration
      • expandChannels
      • expandTranslator
      • expandOther
      • expandDatabase
      • expandAdministration
      • expandLogs
      • expandChameleon
    • expandTips
      • expandChannels
      • expandChameleon
      • expandWeb Services
      • expandSecurity
      • expandProgramming
      • expandOther
      • expandAdministration
  • expandReference
    • expandIguana Enterprise and Professional
    • expandProgram Settings
    • expandChannel Settings
    • expandDashboard
    • expandChannels
    • expandTranslator
    • expandLogs
      • expandLog Encryption
    • expandHTTP API
    • expandCDA API
    • expandError Messages
    • expandChameleon
    • expandIguana Change Log

Other Links

  • Training Center
  • News & Announcements
  • iNTERFACEWARE Blog
  • Older Documention (IGUANA v4 & Chameleon)
Copyright © iNTERFACEWARE Inc.