This example shows how to use the age.lua module to calculate age from DOB, it allows for leap years and returns years, months and partial years (i.e., 17, 3, 17.296272).
Note: The age module accepts a wide range of date formats.
If you have any questions please contact us at firstname.lastname@example.org.
Using the Code [top]
- Import the Calculate Age From DOB channel from the Builtin: Iguana Date/Time repository
- Experiment with the code to find out how it works
- Then add the modules to your Translator project
- Copy the require statement from the channel and add it at the top of your script
Note: This module uses require to return a single function
- Adapt the code to your own requirements
- Interactive scripting help is included for this module
This is the github code for the main module:
How it works [top]
First it calculates the age in seconds. Then it converts the seconds to years allowing for leap years. Finally it converts the remaining seconds to months and also a fractional year (if you need greater accuracy). The fractional year calculation also takes into account if the current year is a leap year.
The date.parse module is used internally to convert date formats, this means that a wide range of date formats are automatically supported. The
os.difftime() function is used to get the differences between dates, as directly subtracting dates does not work.
More information [top]
- Source code for the main module on github
- Source code for the date.age.lua module on github
- Source code for the date.parse.lua module on github