模組:沙盒/Dabao qian/ISODate

local getArgs = require('Module:Arguments').getArgs
local p = {}

function p.dates(frame)
	local returnval;
	local args = getArgs(frame);
	
	args['1'] = args['1']:gsub(" "," ");
	
	local ym = false;
	local yearonly = false;
	local y, m, d = string.match(args['1'], '(%d+)年(%d+)月(%d+)日');
	if d == nil then
		ym = true;
		y, m = string.match(args['1'], '(%d+)年(%d+)月');
	end
	if d == nil and m == nil then
		yearonly = true;
		y = string.match(args['1'], '(%d+)年');
	end
	
	if y ~= nil then
		if ym == false then
			returnval = y..'-'..string.rep(0, 2 - #m)..m..'-'..string.rep(0, 2 - #d)..d;
		else
			if yearonly == false then
				returnval = y..'-'..string.rep(0, 2 - #m)..m;
			else
				returnval = y;
			end
		end
	else
		returnval = args['1'];	
	end
	
	return returnval;
end

return p