Module:TimeAgo: Difference between revisions
Appearance
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..." |
No edit summary |
||
(One intermediate revision by the same user not shown) | |||
Line 1: | Line 1: | ||
local p = {} | local p = {} | ||
-- Function to calculate | -- Function to format the date and calculate time difference | ||
function p.timeAgo(frame) | function p.timeAgo(frame) | ||
local founded = frame.args[1] | local founded = frame.args[1] | ||
Line 27: | Line 27: | ||
end | end | ||
-- Return the | -- Format the founded date (e.g., "January 14th, 2024") | ||
return yearDiff .. " year(s) ago" | 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 | end | ||
return p | return p |
Latest revision as of 02:30, 14 January 2025
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