Modul:Convertdate
Utseende
Dokumentationen för denna modul kan skapas på Modul:Convertdate/dok
-- Testing with the Debug console:
-- frame = {}
-- frame.args = {"April 2014"}
-- =p.str2num(frame)
local p = {}
-- Months in English and Swedish
months = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"}
manader = {"januari", "februari", "mars", "april", "maj", "juni", "juli", "augusti", "september", "oktober", "november", "december"}
function p.num2str(frame)
local arguments = frame.args
local datestr = arguments[1]
-- Assume ISO 8601 format
local year,month,day = string.match(datestr,"(%d%d%d%d)-(%d?%d)-(%d?%d)")
if not year then
year,month = string.match(datestr,"(%d%d%d%d)-(%d?%d)")
if not year then
-- No clue what the input is. Return unchanged.
return datestr
end
end
month = tonumber(month)
if month < 1 or month > 12 then
-- Not a month. Return unchanged.
return datestr
end
datestr = manader[month] .. " " .. year
if day then
datestr = day .. " " .. datestr
end
return datestr
end
function p.str2num(frame)
local year = 0
local arguments = frame.args
local datestr = arguments[1]
-- Search for matching month
local s = string.match(datestr, "%a+")
for i = 1, (#months-1) do
if s == months[i] or s == manader[i] then
-- Also need the year
year = string.match(datestr, "%d%d%d%d")
if year then
-- OK, replace it
day = string.match(datestr, "%d%d? ")
-- Fix leading zero
if i > 9 then
mon = i
else
mon = "0" .. i
end
datestr = year .. "-" .. mon
if day then
if tonumber(day) < 10 then
day = "0" .. day
end
datestr = datestr .. "-" .. day
end
end
end
end
return datestr
end
return p