Modul:Formatiraj datum
Upotreba
U prvi se parametar unosi datum u formatu dan s točkom / mjesec u genitivu / godina s točkom, a u drugom se parametru definira HTML klasa.
Primjer upotrebe za datume rođenja:
{{#invoke:Formatiraj datum|main|2. studenoga 2025.|bday}} → 2. studenoga 2025.
<span style="display:none" class="bday">2025-11-02</span> 2. studenoga 2025.
Osim klase bday, mogu se dodati i dtstart (oznaka početka događaja) ili dtend (oznaka završetka događaja).
Funkcije
- getISO
- Koristi se za izvoz datuma u ISO formatu radi daljnje obrade u drugim modulima; npr. Modul:Datum i dob koristi izvezeni datum za izračun dobi osobe.
- formatDateWithClass
- Generira skriveni HTML element s datumom u ISO formatu i zadanom klasom (npr. bday), radi strojne čitljivosti.
Bilješke
Modul ne prikazuje greške i ne radi ništa što je vidljivo na stranici, tj. uvijek vraća cijeli unos, no ne formatira datum ako se oko njega nalazi suvišan tekst ili ako je unos datuma nepravilan.
{{#invoke:Formatiraj datum|main|Zagreb, 19. rujna 1960.}}
{{#invoke:Formatiraj datum|main|2.11.2025.}}
Dodavanje izvora nakon datuma je podržano.
{{#invoke:Formatiraj datum|main|30. kolovoza 1960.<ref>...</ref>}} → 30. kolovoza 1960.[1]
U svrhu daljnje obrade datuma (npr. za izračune), modul prepoznaje i nepotpune datume (npr. studeni 2025. ili samo 2025.), no ne dodaje im klasu.
Vidi još
local p = {}
local mjeseci = {
-- Genitiv
["siječnja"] = "01", ["veljače"] = "02", ["ožujka"] = "03", ["travnja"] = "04",
["svibnja"] = "05", ["lipnja"] = "06", ["srpnja"] = "07", ["kolovoza"] = "08",
["rujna"] = "09", ["listopada"] = "10", ["studenoga"] = "11", ["studenog"] = "11", ["prosinca"] = "12",
-- Nominativ
["siječanj"] = "01", ["veljača"] = "02", ["ožujak"] = "03", ["travanj"] = "04",
["svibanj"] = "05", ["lipanj"] = "06", ["srpanj"] = "07", ["kolovoz"] = "08",
["rujan"] = "09", ["listopad"] = "10", ["studeni"] = "11", ["prosinac"] = "12"
}
-- Broj dana u mjesecima
local daniUMjesecu = {["01"] = 31, ["02"] = 28, ["03"] = 31, ["04"] = 30, ["05"] = 31, ["06"] = 30, ["07"] = 31, ["08"] = 31, ["09"] = 30, ["10"] = 31, ["11"] = 30, ["12"] = 31}
-- Provjerava je li godina prijestupna
local function prijestupna(godina)
return (godina % 4 == 0 and godina % 100 ~= 0) or (godina % 400 == 0) end
-- Glavna funkcija za obradu datuma
local function dohvatiISO(DATUMstring)
local CISTIdatum = DATUMstring
CISTIdatum = mw.text.killMarkers(CISTIdatum)
CISTIdatum = string.gsub(CISTIdatum, "%[%[([^%]]+)%]%]", "%1")
CISTIdatum = mw.text.trim(CISTIdatum)
local dan, mjesec, godina = CISTIdatum:match("^(%d+)%.%s+([%ačćžšđ]+)%s+(%d+)%.$")
if dan and mjesec and godina then
local BROJgodina = tonumber(godina)
if not BROJgodina or BROJgodina < 1000 or BROJgodina > 9999 then return nil end
local BROJmjesec = mjeseci[mjesec:lower()]
if not BROJmjesec then return nil end
local MAXdana = daniUMjesecu[BROJmjesec]
if BROJmjesec == "02" and prijestupna(BROJgodina) then MAXdana = 29 end
local BROJdan = tonumber(dan)
if not BROJdan or BROJdan < 1 or BROJdan > MAXdana then return nil end
local FORMATIRANIdan = string.format("%02d", BROJdan)
return godina .. "-" .. BROJmjesec .. "-" .. FORMATIRANIdan
end
local mjesec2, godina2 = CISTIdatum:match("^([%ačćžšđ]+)%s+(%d+)%.$")
if mjesec2 and godina2 then
local BROJgodina2 = tonumber(godina2)
if not BROJgodina2 or BROJgodina2 < 1000 or BROJgodina2 > 9999 then return nil end
local BROJmjesec2 = mjeseci[mjesec2:lower()]
if not BROJmjesec2 then return nil end
return godina2 .. "-" .. BROJmjesec2
end
local godina3 = CISTIdatum:match("^(%d+)%.$")
if godina3 then
local BROJgodina3 = tonumber(godina3)
if not BROJgodina3 or BROJgodina3 < 1000 or BROJgodina3 > 9999 then return nil end
return godina3
end
return nil
end
function p.getISO(frame)
local DATUMstring = frame.args[1] or ""
return dohvatiISO(DATUMstring)
end
p.dohvatiISO = dohvatiISO
-- Dodaj class (bday/dtstart/dtend)
function p.formatDateWithClass(frame)
local DATUMstring = frame.args[1] or ""
local class = frame.args[2] or ""
if class ~= "bday" and class ~= "dtstart" and class ~= "dtend" then return "" end
local ISOdatum = dohvatiISO(DATUMstring)
if not ISOdatum then return "" end
if string.len(ISOdatum) == 10 then
local SPAN = string.format('<span style="display:none" class="%s">%s</span>', class, ISOdatum)
return SPAN
end
return ""
end
function p.main(frame)
local args = frame.args
local UNESENIdatum = args[1] or ""
local class = args[2] or ""
local SPAN = p.formatDateWithClass{ args = { [1] = UNESENIdatum, [2] = class } }
if SPAN == "" then
return UNESENIdatum
end
return SPAN .. " " .. UNESENIdatum
end
-- Izvoz funkcija za druge module
local function getMaxDan(godina, mjesec)
local max = daniUMjesecu[mjesec]
if mjesec == "02" and prijestupna(godina) then
max = 29
end
return max
end
p.daniUMjesecu = daniUMjesecu
p.jePrestupna = prijestupna
p.getMaxDan = getMaxDan
-- Funkcija za televizijske predloške
-- Učitava datum s sadržajem u zagradi
function p.televizija(frame)
local args = frame.args
local UNESENIdatum = args[1] or ""
local class = args[2] or ""
if class ~= "bday" and class ~= "dtstart" and class ~= "dtend" then
return UNESENIdatum
end
local datePart, parentheticalPart = string.match(UNESENIdatum, "^(.-)%s+%(([^)]+)%)s*$")
local originalDateForDisplay
if datePart then
originalDateForDisplay = mw.text.trim(datePart)
else
datePart = UNESENIdatum
originalDateForDisplay = UNESENIdatum
end
local ISOdatum = dohvatiISO(mw.text.trim(datePart))
if ISOdatum then
-- dodaj span za sve duljine: YYYY, YYYY-MM, YYYY-MM-DD
local SPAN = string.format('<span style="display:none" class="%s">%s</span>', class, ISOdatum)
local result = SPAN .. " " .. originalDateForDisplay
if parentheticalPart then
result = result .. " (" .. parentheticalPart .. ")"
end
return result
else
return UNESENIdatum
end
end
return p