Advertisement

Module:Date and Age

Documentation for this module may be created at Module:Date and Age/doc

local p = {}

--Function used by Template:Birth_Date_and_Age
function p.birth_date_and_age(vars)
	birth_year = tonumber(vars.args[1])
	birth_month = tonumber(vars.args[2])
	birth_day = tonumber(vars.args[3])
	current_year = tonumber(vars.args[4])
	current_month = tonumber(vars.args[5])
	current_day = tonumber(vars.args[6])
	
	--Format birth date
	date_string = format_date(birth_year,birth_month,birth_day)
	
	--Calculate age and format it
	age = calculate_age(birth_year,birth_month,birth_day,current_year,current_month,current_day)
	age_string = "(age " .. age .. ")"
	
	--Return date and age
	final_string = date_string .. " " .. age_string
	return final_string
end

--Function used by Template:Death_Date_and_Age
function p.death_date_and_age(vars)
	birth_year = tonumber(vars.args[1])
	birth_month = tonumber(vars.args[2])
	birth_day = tonumber(vars.args[3])
	death_year = tonumber(vars.args[4])
	death_month = tonumber(vars.args[5])
	death_day = tonumber(vars.args[6])
	
	--Format death date
	date_string = format_date(death_year,death_month,death_day)
	
	--Calculate age and format it
	age = calculate_age(birth_year,birth_month,birth_day,death_year,death_month,death_day)
	age_string = "(aged " .. age .. ")"
	
	--Return date and age
	final_string = date_string .. " " .. age_string
	return final_string
end
 
--Formats a date
function format_date(year,month,day)
	month_name = get_month_name(month)
	date_string = month_name .. " " .. day .. ", " .. year
	
	return date_string
end

--Converts a month number to the month's name
function get_month_name(month)
	month_names = {"January","Feburary","March","April","May","June","July","August","September","October","November","December"}
	return month_names[month]
end

--Calculates age between two dates
function calculate_age(year1,month1,day1,year2,month2,day2)
	age = year2 - year1
	if ((month2 < month1) or (month2 == month1 and day2 < day1)) then
		age = age - 1
	end
	return age
end

return p