Module:PageDates: Difference between revisions

Created page with "local p = {} function p.dates(frame) local title = mw.title.getCurrentTitle() local firstRev = title:getFirstRevision() local lastRev = title:getLatestRevID() if not firstRev then return "Page history not available." end local createdTimestamp = firstRev.timestamp local updatedTimestamp = title:getTimestamp() local lang = mw.language.getContentLanguage() local createdDate = lang:formatDate("j F Y", createdTimestamp) loc..."
 
No edit summary
 
(7 intermediate revisions by the same user not shown)
Line 1: Line 1:
local p = {}
local p = {}


function p.dates(frame)
-- Properly handle ordinal suffixes
     local title = mw.title.getCurrentTitle()
local function ordinal(n)
     local firstRev = title:getFirstRevision()
     local v = n % 100
     local lastRev = title:getLatestRevID()
    if v >= 11 and v <= 13 then
 
        return n .. "th"
     if not firstRev then
    end
        return "Page history not available."
     local lastDigit = n % 10
     if lastDigit == 1 then return n .. "st"
    elseif lastDigit == 2 then return n .. "nd"
     elseif lastDigit == 3 then return n .. "rd"
    else return n .. "th"
     end
     end
end


     local createdTimestamp = firstRev.timestamp
function p.dates(frame)
     local updatedTimestamp = title:getTimestamp()
     local lang = mw.language.getContentLanguage()
     local timestamp = frame:preprocess("{{REVISIONTIMESTAMP}}")


     local lang = mw.language.getContentLanguage()
    -- Extract date parts
     local createdDate = lang:formatDate("j F Y", createdTimestamp)
     local day = tonumber(lang:formatDate("j", timestamp))
     local updatedDate = lang:formatDate("j F Y", updatedTimestamp)
     local month = lang:formatDate("F", timestamp)
     local year = lang:formatDate("Y", timestamp)


     if createdDate == updatedDate then
     local formatted = string.format("%s %s, %s", month, ordinal(day), year)
        return string.format("It was added on %s.", createdDate)
     return formatted
    else
        return string.format("It was added on %s and revised on or before %s.", createdDate, updatedDate)
     end
end
end


return p
return p