Module:TimeAgo

Revision as of 02:28, 14 January 2025 by Jord (talk | contribs) (Created page with "local p = {} -- Function to calculate the time difference and return it as a string function p.timeAgo(frame) local founded = frame.args[1] local currentDate = mw.date("yyyy-mm-dd") -- Convert the founded date to a Lua date object local foundedDate = mw.text.split(founded, "-") local foundedYear = tonumber(foundedDate[1]) local foundedMonth = tonumber(foundedDate[2]) local foundedDay = tonumber(foundedDate[3]) -- Current date co...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

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

local p = {}

-- Function to calculate the time difference and return it as a string
function p.timeAgo(frame)
    local founded = frame.args[1]
    local currentDate = mw.date("yyyy-mm-dd")
    
    -- Convert the founded date to a Lua date object
    local foundedDate = mw.text.split(founded, "-")
    local foundedYear = tonumber(foundedDate[1])
    local foundedMonth = tonumber(foundedDate[2])
    local foundedDay = tonumber(foundedDate[3])
    
    -- Current date components
    local currentYear = tonumber(mw.date("yyyy"))
    local currentMonth = tonumber(mw.date("mm"))
    local currentDay = tonumber(mw.date("dd"))
    
    -- Calculate the difference in years
    local yearDiff = currentYear - foundedYear
    local monthDiff = currentMonth - foundedMonth
    local dayDiff = currentDay - foundedDay
    
    -- If the current month and day haven't yet passed, subtract one year
    if monthDiff < 0 or (monthDiff == 0 and dayDiff < 0) then
        yearDiff = yearDiff - 1
    end
    
    -- Return the result
    return yearDiff .. " year(s) ago"
end

return p