Toggle menu
Toggle preferences menu
Toggle personal menu
Not logged in
Your IP address will be publicly visible if you make any edits.

Module:TimeAgo

From Bhikitia, An open encyclopedia
Revision as of 13:45, 15 December 2024 by Parvej Husen Talukder (talk | contribs) (Created page with "local p = {} function p.timeago(frame) local date = frame.args[1] or "" if date == "" then return "Invalid date" end local current = os.time() local given = os.time{year=string.sub(date, 1, 4), month=string.sub(date, 6, 7), day=string.sub(date, 9, 10)} local diff = os.difftime(current, given) local days = math.floor(diff / (24 * 60 * 60)) if days < 1 then return "Today" elseif days == 1 then return "1 day ago" elsei...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

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

local p = {}

function p.timeago(frame)
    local date = frame.args[1] or ""
    if date == "" then return "Invalid date" end

    local current = os.time()
    local given = os.time{year=string.sub(date, 1, 4), month=string.sub(date, 6, 7), day=string.sub(date, 9, 10)}

    local diff = os.difftime(current, given)
    local days = math.floor(diff / (24 * 60 * 60))

    if days < 1 then
        return "Today"
    elseif days == 1 then
        return "1 day ago"
    elseif days < 30 then
        return days .. " days ago"
    elseif days < 365 then
        return math.floor(days / 30) .. " months ago"
    else
        return math.floor(days / 365) .. " years ago"
    end
end

return p