モジュール:Age
このモジュールについての説明文ページを モジュール:Age/doc に作成できます
--- 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