Lompat ke isi

Modul:Start date and age

Dari Wikisporta, ensiklopedia olahraga

Dokumentasi untuk modul ini dapat dibuat di Modul:Start date and age/doc

local p = {}

-- Nama bulan dalam bahasa Indonesia
local monthsName = {
    "Januari", "Februari", "Maret", "April", "Mei", "Juni",
    "Juli", "Agustus", "September", "Oktober", "November", "Desember"
}

-- Fungsi untuk menghitung selisih tahun secara presisi
local function calculateYears(bYear, bMonth, bDay)
    local today = os.date("*t")
    local years = today.year - bYear
    
    if today.month < bMonth or (today.month == bMonth and today.day < bDay) then
        years = years - 1
    end
    
    return years
end

function p.main(frame)
    local args = frame:getParent().args
    local year, month, day

    -- Cek jika input menggunakan format YYYY-MM-DD atau YYYY/MM/DD
    local inputStr = args[1] or ""
    if inputStr:find("^%d%d%d%d%-%d%d%-%d%d$") or inputStr:find("^%d%d%d%d/%d%d/%d%d$") then
        year, month, day = inputStr:match("(%d%d%d%d)[%-/](%d%d)[%-/](%d%d)")
        year = tonumber(year)
        month = tonumber(month)
        day = tonumber(day)
    else
        year = tonumber(args[1] or args.year)
        month = tonumber(args[2] or args.month)
        day = tonumber(args[3] or args.day)
    end

    -- Validasi Tanggal
    if not year or not month or not day or month < 1 or month > 12 or day < 1 or day > 31 then
        return '<span style="color:red; font-weight:bold;">[Format Tanggal Salah]</span>'
    end

    local years = calculateYears(year, month, day)
    local monthName = monthsName[month] or ""

    -- Teks durasi
    local durationStr = string.format('%d tahun yang lalu', years)

    -- Format Output Rapi:
    -- Baris 1: 29 November 1899
    -- Baris 2: (<i>126 tahun yang lalu</i>) -> kurung tegak, teks miring & ukuran 80%
    return string.format('%d %s %d<br /><small style="font-size: 80%%;">(<i>%s</i>)</small>', day, monthName, year, durationStr)
end

return p