Module:Years or months ago

Module documentation
local Date = require('Module:Date')._Date

local lang = mw.getCurrentFrame().args[1] or 'en' --{{PAGELANGUAGE}}
local JsonStrings = mw.title.new('Template:Years or months ago/l10n.json/' .. lang):getContent()
		or mw.title.new('Template:Years or months ago/l10n.json/en'):getContent()
local strings = mw.text.jsonDecode(JsonStrings)

local p = {}

function p.eval (moduleFrame)
	local templateFrame = moduleFrame:getParent()
	
	local date1, date2 =
		Date(
			templateFrame.args[1], --year
			templateFrame.args[2] or os.date('!*t').month, 1 --month (current by default)
		) or Date('currentdate'), 
		Date(
			templateFrame.args[3], --year
			templateFrame.args[4] or os.date('!*t').month, 1 --month (current by default)
		) or Date('currentdate')
	
	if(date1 > date2) then
		local datediff = date1 - date2
		if (datediff.years >= 1) then
			return templateFrame:preprocess( mw.ustring.format(
				strings['n_years_time'], datediff.years, datediff.years
			) )
			--'{{formatnum:' .. datediff.years .. '}} {{PLURAL:' .. datediff.years .. '|year’s|years’}} time'
		else
			return templateFrame:preprocess( mw.ustring.format(
				strings['n_months_time'], datediff.months, datediff.months
			) )
			--'{{formatnum:' .. datediff.months .. '}} {{PLURAL:' .. datediff.months .. '|month’s|months’}} time'
		end
	else --date1 is before date2
		local datediff = date2 - date1
		if (datediff.years >= 1) then
			return templateFrame:preprocess( mw.ustring.format(
				strings['n_years_ago'], datediff.years, datediff.years
			) )
			--'{{formatnum:' .. datediff.years .. '}} {{PLURAL:' .. datediff.years .. '|year|years}} ago'
		else
			return templateFrame:preprocess( mw.ustring.format(
					strings['n_months_ago'],datediff.months, datediff.months
			) )
			--'{{formatnum:' .. datediff.months .. '}} {{PLURAL:' .. datediff.months .. '|month|months}} ago'
		end
	end
end

return p