Jump to content

Module:DayCounter

From SpongeBob Wiki

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

local p = {}

function p.daysSince(frame)
    local args = frame.args
    local startDate = args.start
    
    if not startDate or startDate == "" then
        return "Error: No start date provided! Use {{#invoke:DayCounter|daysSince|start=YYYY-MM-DD}}"
    end

    local year, month, day = startDate:match("(%d+)-(%d+)-(%d+)")
    year, month, day = tonumber(year), tonumber(month), tonumber(day)

    if not (year and month and day) then
        return "Error: Invalid date format! Use YYYY-MM-DD."
    end

    local currentTimestamp = tonumber(frame:preprocess("{{#time:U|now}}")) 

    local startTimestamp = tonumber(frame:preprocess("{{#time:U|" .. startDate .. " 00:00:00 UTC}}")) 

    local diff = math.floor((currentTimestamp - startTimestamp) / 86400)

    return frame:preprocess("<!-- " .. os.date("%Y-%m-%d") .. " -->") .. diff
end

return p