Modul:DatumFormatierung

Aus Stadtsprachen
Wechseln zu:Navigation, Suche

Die Dokumentation für dieses Modul kann unter Modul:DatumFormatierung/Doku erstellt werden

local p = {}

-- Helper function to split string by delimiter
local function split(str, delimiter)
    local result = {}
    for match in (str..delimiter):gmatch("(.-)"..delimiter) do
        table.insert(result, match)
    end
    return result
end

-- Main function to transform date formats
function p.formatDate(frame)
    local date = frame.args[1] or ''
    
    -- If empty, return empty string
    if date == '' then
        return ''
    end
    
    -- Replace any forward slashes with hyphens for consistent processing
    date = string.gsub(date, '/', '-')
    
    -- Split the date string
    local parts = split(date, '-')
    
    -- Get the year (will always be first)
    local year = parts[1]
    
    -- Handle different cases based on number of parts
    if #parts == 1 then
        -- Only year provided
        return year
    elseif #parts == 2 then
        -- Year and month provided
        local month = parts[2]
        return month .. '.' .. year
    elseif #parts == 3 then
        -- Full date provided
        local month = parts[2]
        local day = parts[3]
        return day .. '.' .. month .. '.' .. year
    else
        -- Invalid format, return original string
        return date
    end
end

return p