Modül:Age
Used to calculate ages of persons given their birth date.
age.age(frame)(function)- Calculates the age of a person given their birth date.
- Parameter:
frameScribunto frame object (table) - Returns: Age of the person (number|string)
valid_date(d)(function • local)- Checks whether a given date string is a valid date.
- Parameter:
dDate string to check (string) - Returns: Whether the string is a valid date (boolean)
--- Used to calculate ages of persons given their birth date.
-- @file age
require('strict')
local age = {}
local Date = require('Module:Date')
--- Checks whether a given date string is a valid date.
-- @function valid_date
-- @param {string} d Date string to check
-- @return {bool} Whether the string is a valid date
-- @local
local function validDate(d)
return pcall(function()
Date(d)
end)
end
--- Calculates the age of a person given their birth date.
-- @function age.age
-- @param {table} frame Scribunto frame object
-- @returns {number|string} Age of the person
function age.age(frame)
local inputDate = mw.text.killMarkers(frame.args[1])
if not validDate(inputDate) then
return ''
end
local date = Date(inputDate)
local currentDate = Date(os.date())
return (currentDate - date):getyear() - 1
end
return age