Module:Date difference: Difference between revisions

From NPOWiki
Jump to navigation Jump to search
create
 
use error module
 
Line 1: Line 1:
local err = require('Module:Error').call
local time = mw.language.getContentLanguage()
local time = mw.language.getContentLanguage()
local p = {}
local p = {}
Line 12: Line 13:
local diff
local diff
if not args[1] or args[1] == '' then
if not args[1] or args[1] == '' then
return '<span class="error">Date: First parameter required</span>'
return err('Date: First parameter required')
end
end
if args[3] and args[3] ~= '' then
if args[3] and args[3] ~= '' then
return '<span class="error">Date: Too many parameters</span>'
return err('Date: Too many parameters')
end
end
if args[2] and args[2] ~= '' then
if args[2] and args[2] ~= '' then
Line 27: Line 28:
isValid, startUnix = pcall(time.formatDate, time, 'U', startDate)
isValid, startUnix = pcall(time.formatDate, time, 'U', startDate)
if not isValid then
if not isValid then
return '<span class="error">Invalid date: ' .. startDate .. '</span>'
return err('Invalid date: ' .. startDate)
end
end
isValid, endUnix = pcall(time.formatDate, time, 'U', endDate)
isValid, endUnix = pcall(time.formatDate, time, 'U', endDate)
if not isValid then
if not isValid then
return '<span class="error">Invalid date: ' .. endDate .. '</span>'
return err('Invalid date: ' .. endDate)
end
end
startUnix = tonumber(startUnix)
startUnix = tonumber(startUnix)
endUnix = tonumber(endUnix)
endUnix = tonumber(endUnix)
if startUnix > endUnix then
if startUnix > endUnix then
return '<span class="error">Invalid date range: ' .. startDate .. ' to ' .. endDate .. '</span>'
return err('Invalid date range: ' .. startDate .. ' to ' .. endDate)
end
end
diff = endUnix - startUnix
diff = endUnix - startUnix

Latest revision as of 11:14, 21 October 2025

Documentation [edit]

This page is a stub - it only covers the very basics of its subject. More information should be added to make the page informative and useful. If no more information is available, the page should be considered for a merge, redirect, or deletion.

Details: documentation to be written

If you can correct the issue, please edit the page to do so, then remove this notice.
Please see Template:Date difference for usage information. Use the template rather than invoking the module.

The above documentation is transcluded from Module:Date difference/doc.

local err = require('Module:Error').call
local time = mw.language.getContentLanguage()
local p = {}

function p.main(frame)
	return p.call(frame:getParent().args)
end

function p.call(args)
	local startDate, startUnix
	local endDate, endUnix
	local isValid
	local diff
	if not args[1] or args[1] == '' then
		return err('Date: First parameter required')
	end
	if args[3] and args[3] ~= '' then
		return err('Date: Too many parameters')
	end
	if args[2] and args[2] ~= '' then
		endDate = args[2]
	else
		endDate = ''
	end
	startDate = args[1]
	--formatDate works same as #time, U is Unix time. Have to pass instance to pcall.
	--Passing empty string gets current time.
	isValid, startUnix = pcall(time.formatDate, time, 'U', startDate)
	if not isValid then
		return err('Invalid date: ' .. startDate)
	end
	isValid, endUnix = pcall(time.formatDate, time, 'U', endDate)
	if not isValid then
		return err('Invalid date: ' .. endDate)
	end
	startUnix = tonumber(startUnix)
	endUnix = tonumber(endUnix)
	if startUnix > endUnix then
		return err('Invalid date range: ' .. startDate .. ' to ' .. endDate)
	end
	diff = endUnix - startUnix
	return math.floor(diff / 86400)
end

return p