Module:Birthagemonth

From Catpedia

Documentation for this module may be created at Module:Birthagemonth/doc

local p = {}

function p.formatDate(frame)
    local month = frame.args[1] or 1
    local year = frame.args[2] or 1970
    return mw.getContentLanguage():formatDate('F Y', string.format('%d-%d', year, month))
end

function p.calculateAge(frame)
    local month = tonumber(frame.args[1]) or 1
    local year = tonumber(frame.args[2]) or 1970

    local currentMonth = tonumber(mw.getCurrentFrame():callParserFunction('#time', 'n'))
    local currentYear = tonumber(mw.getCurrentFrame():callParserFunction('#time', 'Y'))

    local age = currentYear - year
    if currentMonth < month then
        age = age - 1
    end

    return age
end

function p.displayDateWithAge(frame)
    local date = p.formatDate(frame)
    local age = p.calculateAge(frame)
    return string.format('%s (age %d)', date, age)
end

return p