Module:TimeAgo
Appearance
Documentation for this module may be created at Module:TimeAgo/doc
local p = {}
-- Function to format the date and calculate time difference
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
-- Format the founded date (e.g., "January 14th, 2024")
local monthNames = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"}
local monthName = monthNames[foundedMonth]
local daySuffix = "th"
if foundedDay == 1 or foundedDay == 21 or foundedDay == 31 then
daySuffix = "st"
elseif foundedDay == 2 or foundedDay == 22 then
daySuffix = "nd"
elseif foundedDay == 3 or foundedDay == 23 then
daySuffix = "rd"
end
local formattedDate = monthName .. " " .. foundedDay .. daySuffix .. ", " .. foundedYear
-- Return the formatted date with the "time ago" text
return formattedDate .. " (" .. yearDiff .. " year(s) ago)"
end
return p