编辑代码


function timediff(long_time, short_time, return_type)
	return_type = return_type or 'day'
	local otab = os.date("*t", short_time)
	local ntab = os.date("*t", long_time)

	local temp = { 1, 12, 30 }
	local res = 0
	for i, name in ipairs({ 'year', 'month' }) do
		res = res * temp[i] + (ntab[name] - otab[name])
		if name == return_type then
			return res
		end
	end
	if return_type == 'day' then
		local n_day
		local days = 0
		for i = 1, res do
			local t_m = otab.month + (i - 1)
			local t_y = otab.year + math.modf((t_m - 1) / 12)
			t_m = t_m % 12 == 0 and 12 or t_m % 12
			days = days + getDaysWithMonth(t_m, t_y)
		end
		res = days + ntab['day'] - otab['day']
		return res
	end
	--处理 小时、分钟、秒
	local temp = { 24, 60, 60 }
	for i, name in ipairs({ 'hour', 'min', 'sec' }) do
		res = res * temp[i] + (ntab[name] - otab[name])
		if name == return_type then
			return res
		end
	end
end