btw this Wiki is not meant for a wider audience yet
Module:YYMMDDSentence1: Difference between revisions
Jump to navigation
Jump to search
m Remove category from Lua module (breaks code) Tag: Manual revert |
(No difference)
|
Latest revision as of 00:00, 6 December 2025
local p = {}
function p.generate(frame)
local dateStr = frame.args[1] or mw.title.getCurrentTitle().text
-- Determine if it's YYMMDD (6 digits) or YYYYMMDD (8 digits)
local yyyy, mm, dd
if #dateStr == 8 then
-- YYYYMMDD format
yyyy = tonumber(dateStr:sub(1, 4))
mm = tonumber(dateStr:sub(5, 6))
dd = tonumber(dateStr:sub(7, 8))
elseif #dateStr == 6 then
-- YYMMDD format
local yy = tonumber(dateStr:sub(1, 2))
mm = tonumber(dateStr:sub(3, 4))
dd = tonumber(dateStr:sub(5, 6))
yyyy = 2000 + yy
else
return "Error: Invalid date format. Expected YYMMDD or YYYYMMDD."
end
-- Validate input
if not yyyy or not mm or not dd then
return "Error: Invalid date format."
end
if mm < 1 or mm > 12 then
return "Error: Invalid month."
end
if dd < 1 or dd > 31 then
return "Error: Invalid day."
end
-- Month names
local monthNames = {
"January", "February", "March", "April", "May", "June",
"July", "August", "September", "October", "November", "December"
}
-- Day of week names
local dayNames = {
"Sunday", "Monday", "Tuesday", "Wednesday",
"Thursday", "Friday", "Saturday"
}
-- Ordinal names
local ordinals = {
"first", "second", "third", "fourth", "fifth"
}
-- Calculate day of week using os.date
local success, timestamp = pcall(os.time, {year = yyyy, month = mm, day = dd, hour = 12})
if not success then
return "Error: Invalid date."
end
local weekday = tonumber(os.date("%w", timestamp)) + 1 -- 1-7, Sunday=1
-- Calculate which occurrence of this weekday in the month
local occurrence = math.ceil(dd / 7)
-- Get month name
local monthName = monthNames[mm]
local dayName = dayNames[weekday]
local ordinal = ordinals[occurrence]
-- Format day with leading zero
local dayFormatted = string.format("%02d", dd)
-- Build the sentence (no punctuation at the end)
local sentence = string.format(
"%s %s %d was the %s %s of the month",
monthName, dayFormatted, yyyy, ordinal, dayName
)
return sentence
end
return p