Jump to content

Module:Last month

Permanently protected module
From Wikisource

--[=[
Loigic for [[Template:last month]]
]=]

local p = {} --p stands for package
local getArgs = require('Module:Arguments').getArgs

--[=[
Function docs
]=]
function p.last_month(frame)
	local args = getArgs(frame)
	
	if args.year == nil or args.month == nil then
		error("Set both year and month args")
	end
	
	if args.day == nil then
		args.day = 1
	end
	
	if args.n == nil then
		args.n = 1
	end
	
	if args.format == nil then
		args.format = "%Y-%m"
	end
	
	local month_index = (args.year * 12) + args.month - args.n
	local year = math.floor(month_index / 12)
	local month = month_index - (year * 12)
	
	local time_str = os.date( "!" .. args.format, os.time({
		year = year,
		month = month,
		day = args.day
	}))
	return time_str
end

return p