Module:Deathage

From Catpedia
Revision as of 19:05, 7 June 2024 by Ethar (talk | contribs) (Created page with "-- DeathAgeDayMonthYear.lua local p = {} function p.formatDate(frame) local day = frame.args[1] or 1 local month = frame.args[2] or 1 local year = frame.args[3] or 1970 return mw.getContentLanguage():formatDate('j F Y', string.format('%d-%d-%d', year, month, day)) end function p.calculateAge(frame) local day = tonumber(frame.args[1]) or 1 local month = tonumber(frame.args[2]) or 1 local year = tonumber(frame.args[3]) or 1970 local curr...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

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

-- DeathAgeDayMonthYear.lua

local p = {}

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

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

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

    local age = currentYear - year
    if (currentMonth < month) or (currentMonth == month and currentDay < day) 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 (aged %d)', date, age)
end

return p