Module:Date converter

local p = {}

local month_names = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"}

local function d_m_y(day, month, year) year = tonumber(year) day = tonumber(day) month = tonumber(month) -- Inverts day by year, if the date format is xxxx/xx/xx if (day and day > 100) then day, year = year, day end

-- Adds two thousand if the date entered is two digits if (year and year < 100) then year = year + 2000 end

-- Inverts day by month, if the date format is month/day/year if (month and (month > 12 and month < 32)) then day, month = month, day end

-- A few pages had a non-existent index error for the month if (month and (month > 12 or month < 1)) then month = nil end

if (day and month and year) ~= nil then return month_names[month] .. " " .. day .. ", " .. year end

return nil end

local function m_y(month, year) year = tonumber(year) month = tonumber(month) -- Returns nil if none of the numbers are greater than 1000 if not year or not month or (year < 1000 and month < 1000) then return nil end -- Inverts month to year, if the date format is year/month if month > 1000 then month, year = year, month end -- A few pages had a non-existent index error for the month if month > 12 or month < 1 then return nil end

if (month and year) ~= nil then return month_names[month] .. " " .. year end

return nil end

function p.main(frame) local date = frame.args[1] local converted local day, month, year = string.match(date, "^(%d+)[/%.%-](%d+)[/%.%-](%d+)$") if day then converted = d_m_y(day, month, year) else month, year = string.match(date, "^(%d+)[/%.%-](%d+)$") converted = m_y(month, year) end return converted or date end

return p