<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://pool.calebcooper.ie/index.php?action=history&amp;feed=atom&amp;title=Module%3ADate%2Fexample</id>
	<title>Module:Date/example - Revision history</title>
	<link rel="self" type="application/atom+xml" href="https://pool.calebcooper.ie/index.php?action=history&amp;feed=atom&amp;title=Module%3ADate%2Fexample"/>
	<link rel="alternate" type="text/html" href="https://pool.calebcooper.ie/index.php?title=Module:Date/example&amp;action=history"/>
	<updated>2026-04-10T23:04:27Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.43.8</generator>
	<entry>
		<id>https://pool.calebcooper.ie/index.php?title=Module:Date/example&amp;diff=13884&amp;oldid=prev</id>
		<title>Caleb Cooper: Created page with &quot;-- Examples showing how to use Module:Date. See the talk page for output. local Date = require(&#039;Module:Date&#039;)._Date  local show  -- function defined below to display resul...&quot;</title>
		<link rel="alternate" type="text/html" href="https://pool.calebcooper.ie/index.php?title=Module:Date/example&amp;diff=13884&amp;oldid=prev"/>
		<updated>2020-10-24T20:50:30Z</updated>

		<summary type="html">&lt;p&gt;Created page with &amp;quot;-- Examples showing how to use &lt;a href=&quot;/Module:Date&quot; title=&quot;Module:Date&quot;&gt;Module:Date&lt;/a&gt;. See the talk page for output. local Date = require(&amp;#039;Module:Date&amp;#039;)._Date  local show  -- function defined below to display resul...&amp;quot;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;-- Examples showing how to use [[Module:Date]]. See the talk page for output.&lt;br /&gt;
local Date = require(&amp;#039;Module:Date&amp;#039;)._Date&lt;br /&gt;
&lt;br /&gt;
local show  -- function defined below to display results&lt;br /&gt;
&lt;br /&gt;
-- A date can be constructed using various input formats.&lt;br /&gt;
local function make_a_date()&lt;br /&gt;
	show(&amp;#039;Make a date&amp;#039;)&lt;br /&gt;
	local same_dates = {&lt;br /&gt;
		Date(2016, 3, 9),&lt;br /&gt;
		Date(&amp;#039;2016-03-09&amp;#039;),&lt;br /&gt;
		Date(&amp;#039;2016-3-9&amp;#039;),&lt;br /&gt;
		Date(&amp;#039;9&amp;amp;nbsp;March 2016&amp;#039;),&lt;br /&gt;
		Date(&amp;#039;09 mar 2016&amp;#039;),&lt;br /&gt;
		Date(&amp;#039;MAR 09 2016&amp;#039;),&lt;br /&gt;
		Date(&amp;#039;March 9, 2016&amp;#039;),&lt;br /&gt;
		Date(&amp;#039;March 9, 2016 CE&amp;#039;),&lt;br /&gt;
		Date(&amp;#039;March 9, 2016 A.D.&amp;#039;),&lt;br /&gt;
		Date(&amp;#039;3:45 pm 9 March 2016&amp;#039;),&lt;br /&gt;
		Date(&amp;#039;3:45 p.m. 9 March 2016&amp;#039;),&lt;br /&gt;
		Date(2016, 3, 9, 15, 45),&lt;br /&gt;
		Date(&amp;#039;9 March 2016 15:45&amp;#039;),&lt;br /&gt;
	}&lt;br /&gt;
	for _, date in ipairs(same_dates) do&lt;br /&gt;
		show(nil, date:text() .. &amp;#039; or &amp;#039; .. date:text(&amp;#039;mdy&amp;#039;))&lt;br /&gt;
	end&lt;br /&gt;
	local more_dates = {&lt;br /&gt;
		Date(&amp;#039;4 October 1582&amp;#039;, &amp;#039;Julian&amp;#039;),&lt;br /&gt;
		Date(&amp;#039;juliandate&amp;#039;, 2299160),&lt;br /&gt;
		Date(&amp;#039;15 October 1582&amp;#039;),&lt;br /&gt;
	}&lt;br /&gt;
	for _, date in ipairs(more_dates) do&lt;br /&gt;
		show(nil, date.dayname .. &amp;#039; &amp;#039; .. date:text() .. &amp;#039; in the &amp;#039; ..&lt;br /&gt;
			date.calendar .. &amp;#039; calendar is Julian day &amp;#039; .. date.jd)&lt;br /&gt;
	end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
-- A date can be displayed using various output formats.&lt;br /&gt;
local function show_a_date()&lt;br /&gt;
	show(&amp;#039;Show a date&amp;#039;)&lt;br /&gt;
	local dates = {&lt;br /&gt;
		Date(2016, 3, 9),&lt;br /&gt;
		Date(&amp;#039;9 March 2016 BC&amp;#039;),&lt;br /&gt;
	}&lt;br /&gt;
	local format_option = {&lt;br /&gt;
		{ &amp;#039;ymd&amp;#039; },&lt;br /&gt;
		{ &amp;#039;mdy&amp;#039; },&lt;br /&gt;
		{ &amp;#039;dmy&amp;#039; },&lt;br /&gt;
		{ &amp;#039;dmy&amp;#039;, &amp;#039;era=B.C.E.&amp;#039; },&lt;br /&gt;
		{ &amp;#039;%A %B %-d, %Y %{era}&amp;#039; },&lt;br /&gt;
		{ &amp;#039;%A %B %-d, %Y %{era}&amp;#039;, &amp;#039;era=A.D.&amp;#039; },&lt;br /&gt;
		{ &amp;#039;a %{dayname} in %{monthname} %Y %{era}&amp;#039; },&lt;br /&gt;
	}&lt;br /&gt;
	for _, t in ipairs(format_option) do&lt;br /&gt;
		local format = t[1]&lt;br /&gt;
		local option = t[2]&lt;br /&gt;
		for _, date in ipairs(dates) do&lt;br /&gt;
			show(nil, date:text(format, option))&lt;br /&gt;
		end&lt;br /&gt;
	end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
-- When an input date is parsed, its format is stored.&lt;br /&gt;
local function keep_format()&lt;br /&gt;
	show(&amp;#039;Keep format of input date&amp;#039;)&lt;br /&gt;
	local somedate = Date(&amp;#039;March 9, 2016&amp;#039;)&lt;br /&gt;
	local dates = {&lt;br /&gt;
		Date(2016, 3, 9),&lt;br /&gt;
		Date(&amp;#039;2016-3-9&amp;#039;),&lt;br /&gt;
		Date(&amp;#039;9&amp;amp;nbsp;Mar 2016&amp;#039;),&lt;br /&gt;
		Date(&amp;#039;March 9, 2016&amp;#039;),&lt;br /&gt;
		Date(somedate, {day = 1}),  -- somedate with day changed&lt;br /&gt;
		somedate + 23,              -- 23 days after somedate&lt;br /&gt;
		somedate - &amp;#039;3 months&amp;#039;,      -- 3 months before somedate&lt;br /&gt;
		Date(&amp;#039;3:45&amp;amp;nbsp;p.m. March&amp;amp;nbsp;9, 2016&amp;#039;),&lt;br /&gt;
	}&lt;br /&gt;
	show(nil, &amp;#039;Format of somedate was &amp;#039; .. somedate.format)&lt;br /&gt;
	for _, date in ipairs(dates) do&lt;br /&gt;
		show(nil, date:text() .. &amp;#039; or &amp;#039; .. date:text(date.format))&lt;br /&gt;
	end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
-- Using Date to get the current date, or current date and time.&lt;br /&gt;
local function current_date()&lt;br /&gt;
	local now_date = Date(&amp;#039;currentdate&amp;#039;)&lt;br /&gt;
	local now_datetime = Date(&amp;#039;currentdatetime&amp;#039;)&lt;br /&gt;
	show(&amp;#039;Current date showing when this page was last purged&amp;#039;,&lt;br /&gt;
		now_date:text(),                -- 7 March 2016 (for example)&lt;br /&gt;
		now_date:text(&amp;#039;mdy&amp;#039;),           -- March 7, 2016&lt;br /&gt;
		now_date:text(&amp;#039;ymd&amp;#039;),           -- 2016-03-07&lt;br /&gt;
		now_date:text(&amp;#039;%A %-d %B %-Y&amp;#039;), -- Monday 7 March 2016&lt;br /&gt;
		now_datetime:text(),            -- 21:32:45 7 March 2016&lt;br /&gt;
		now_datetime:text(&amp;#039;hms&amp;#039;),       -- 21:32:45&lt;br /&gt;
		now_datetime:text(&amp;#039;%c&amp;#039;)         -- 9:32 pm 7 March 2016&lt;br /&gt;
	)&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
-- Using current to provide default values.&lt;br /&gt;
local function current_as_default(year, month, day, hour, minute, second)&lt;br /&gt;
	local current = require(&amp;#039;Module:Date&amp;#039;)._current&lt;br /&gt;
	year = year or current.year&lt;br /&gt;
	month = month or current.month&lt;br /&gt;
	day = day or current.day&lt;br /&gt;
	hour = hour or current.hour&lt;br /&gt;
	minute = minute or current.minute&lt;br /&gt;
	second = second or current.second&lt;br /&gt;
	show(&amp;#039;Using the current date as a default&amp;#039;,&lt;br /&gt;
		year, month, day, hour, minute, second)&lt;br /&gt;
	-- Alternatively, a date can be constructed with specified items overridden.&lt;br /&gt;
	show(nil, Date(&amp;#039;currentdatetime&amp;#039;, {&lt;br /&gt;
		year = year,&lt;br /&gt;
		month = month,&lt;br /&gt;
		day = day,&lt;br /&gt;
		hour = hour,&lt;br /&gt;
		minute = minute,&lt;br /&gt;
		second = second }):text(&amp;#039;%c&amp;#039;))&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
-- Make a date from the day number in a year.&lt;br /&gt;
local function date_from_day_of_year()&lt;br /&gt;
	-- Example: day 123 in 2015 and in 2016.&lt;br /&gt;
	show(&amp;#039;Make a date from the day number in a year&amp;#039;)&lt;br /&gt;
	local offset = 123 - 1  -- 1 January has day-of-year = 1&lt;br /&gt;
	for _, year in ipairs({ 2015, 2016 }) do&lt;br /&gt;
		local date = Date(year, 1, 1) + offset&lt;br /&gt;
		show(nil, &amp;#039;Day &amp;#039; .. date.dayofyear .. &amp;#039; in &amp;#039; .. year .. &amp;#039; is &amp;#039; .. date:text())&lt;br /&gt;
	end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
-- Number of days in a month for Gregorian (default) and Julian calendars.&lt;br /&gt;
local function days_in_month(year, month, calendar_name)&lt;br /&gt;
	local title = &amp;#039;Days in month&amp;#039;&lt;br /&gt;
	if calendar_name then&lt;br /&gt;
		title = title .. &amp;#039; (&amp;#039; .. calendar_name .. &amp;#039; calendar)&amp;#039;&lt;br /&gt;
	end&lt;br /&gt;
	local monthdays = require(&amp;#039;Module:Date&amp;#039;)._days_in_month&lt;br /&gt;
	show(title, monthdays(year, month, calendar_name))&lt;br /&gt;
	-- Alternative method, using Date.&lt;br /&gt;
	local date = Date(year, month, 1, calendar_name)&lt;br /&gt;
	show(nil, date.monthname .. &amp;#039; &amp;#039; .. date.year .. &amp;#039; had &amp;#039; .. date.monthdays .. &amp;#039; days&amp;#039;)&lt;br /&gt;
	show(nil, date:text(&amp;#039;%{monthname} %{year} had %{monthdays} days&amp;#039;))  -- same&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
-- Julian day number and date arithmetic.&lt;br /&gt;
local function julian_date(year, month, count, calendar_name)&lt;br /&gt;
	if calendar_name then&lt;br /&gt;
		show(&amp;#039;Julian date (&amp;#039; .. calendar_name .. &amp;#039;)&amp;#039;)&lt;br /&gt;
	else&lt;br /&gt;
		show(&amp;#039;Julian date (Gregorian)&amp;#039;)  -- Gregorian calendar by default&lt;br /&gt;
		local date = Date(&amp;#039;24 November 4714 BCE&amp;#039;)&lt;br /&gt;
		show(nil, &amp;#039;Julian day number was &amp;#039; .. date.jd .. &amp;#039; on &amp;#039; .. date:text(&amp;#039;mdy&amp;#039;))&lt;br /&gt;
	end&lt;br /&gt;
	local first_of_month = Date(year, month, 1, calendar_name)&lt;br /&gt;
	for _ = 1, count do&lt;br /&gt;
		first_of_month = first_of_month + &amp;#039;1m&amp;#039;  -- next month&lt;br /&gt;
		local date = first_of_month - &amp;#039;1d&amp;#039;  -- last day of month&lt;br /&gt;
		show(nil,&lt;br /&gt;
			&amp;#039;Last day in month (&amp;#039; .. date:text() .. &amp;#039; &amp;#039; .. date.calendar ..&lt;br /&gt;
			&amp;#039; calendar) had Julian day number &amp;#039; .. date.jd&lt;br /&gt;
		)&lt;br /&gt;
	end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
-- Number of days a date was in the past, or will be in the future.&lt;br /&gt;
local function how_long(date_text)&lt;br /&gt;
	local now_date = Date(&amp;#039;currentdate&amp;#039;)&lt;br /&gt;
	local then_date = Date(date_text)&lt;br /&gt;
	if not then_date then&lt;br /&gt;
		show(&amp;#039;How long&amp;#039;, &amp;#039;Invalid date: &amp;#039; .. (date_text or &amp;#039;&amp;#039;))&lt;br /&gt;
		return&lt;br /&gt;
	end&lt;br /&gt;
	local fmt = &amp;#039;%A %B %-d, %-Y %{era} (day of year = %{dayofyear}, serial day = %{gsd}) &amp;#039;&lt;br /&gt;
	local info = then_date:text(fmt)&lt;br /&gt;
	if then_date == now_date then&lt;br /&gt;
		fmt = &amp;#039;is now (%d day%s)&amp;#039;&lt;br /&gt;
	elseif then_date &amp;gt; now_date then&lt;br /&gt;
		fmt = &amp;#039;will be in %d day%s&amp;#039;&lt;br /&gt;
	else&lt;br /&gt;
		fmt = &amp;#039;was %d day%s ago&amp;#039;&lt;br /&gt;
	end&lt;br /&gt;
	local diff = then_date - now_date&lt;br /&gt;
	local days = diff.age_days&lt;br /&gt;
	local s = days == 1 and &amp;#039;&amp;#039; or &amp;#039;s&amp;#039;&lt;br /&gt;
	show(&amp;#039;How long&amp;#039;, info .. string.format(fmt, days, s))&lt;br /&gt;
	local y, m, d = diff:age(&amp;#039;ymd&amp;#039;)  -- age in years, months, days&lt;br /&gt;
	show(nil, string.format(&amp;#039;(%d years + %d months + %d days)&amp;#039;, y, m, d))&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
-- First and third Fridays in each month of the given year.&lt;br /&gt;
local function fridays(year)&lt;br /&gt;
	show(&amp;#039;First and third Fridays in each month of &amp;#039; .. year)&lt;br /&gt;
	for month = 1, 12 do&lt;br /&gt;
		local dates = Date(year, month, 1):list(&amp;#039;Friday &amp;gt;=&amp;#039;)&lt;br /&gt;
		show(nil, dates[1] .. &amp;#039;, &amp;#039; .. dates[3])&lt;br /&gt;
	end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
-- Next Friday after or before a particular date.&lt;br /&gt;
local function next_friday()&lt;br /&gt;
	show(&amp;#039;Next Friday and last Friday for certain dates&amp;#039;)&lt;br /&gt;
	local dates = {&lt;br /&gt;
		Date(&amp;#039;1 Jan 2016&amp;#039;),&lt;br /&gt;
		Date(&amp;#039;2 Jan 2016&amp;#039;),&lt;br /&gt;
		Date(&amp;#039;3 Mar 1980&amp;#039;),&lt;br /&gt;
	}&lt;br /&gt;
	for _, date in ipairs(dates) do&lt;br /&gt;
		-- 1 = number of Fridays that are wanted in the list&lt;br /&gt;
		local after = date:list(&amp;#039;1 Friday&amp;#039;)[1]&lt;br /&gt;
		local before = date:list(&amp;#039;1 Friday &amp;lt;&amp;#039;)[1]&lt;br /&gt;
		local format = &amp;#039;%A %-d %B %-Y&amp;#039;&lt;br /&gt;
		show(nil,&lt;br /&gt;
			&amp;#039;For &amp;#039; .. date:text(format) ..&lt;br /&gt;
			&amp;#039;, the next is &amp;#039; .. after:text(format) ..&lt;br /&gt;
			&amp;#039;, and the last is &amp;#039; .. before:text(format)&lt;br /&gt;
		)&lt;br /&gt;
	end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
-- Results are held in the lines table.&lt;br /&gt;
local lines&lt;br /&gt;
function show(title, ...)  -- for forward declaration above&lt;br /&gt;
	if title then&lt;br /&gt;
		if lines[1] then&lt;br /&gt;
			table.insert(lines, &amp;#039;&amp;#039;)&lt;br /&gt;
		end&lt;br /&gt;
		table.insert(lines, &amp;quot;&amp;#039;&amp;#039;&amp;#039;&amp;quot; .. title .. &amp;quot;&amp;#039;&amp;#039;&amp;#039;&amp;quot;)&lt;br /&gt;
	end&lt;br /&gt;
	for _, text in ipairs({...}) do&lt;br /&gt;
		table.insert(lines, &amp;#039;:&amp;#039; .. tostring(text))&lt;br /&gt;
	end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
local function main()&lt;br /&gt;
	lines = {}&lt;br /&gt;
	make_a_date()&lt;br /&gt;
	show_a_date()&lt;br /&gt;
	keep_format()&lt;br /&gt;
	current_date()&lt;br /&gt;
	current_as_default(nil, nil, nil, 14, 30, 0)  -- 2:30 pm today&lt;br /&gt;
	date_from_day_of_year()&lt;br /&gt;
	days_in_month(1900, 2)&lt;br /&gt;
	days_in_month(1900, 2, &amp;#039;Julian&amp;#039;)&lt;br /&gt;
	julian_date(1899, 11, 4)&lt;br /&gt;
	julian_date(-120, 11, 4, &amp;#039;Julian&amp;#039;)&lt;br /&gt;
	how_long(&amp;#039;29 Feb 2100&amp;#039;)  -- an invalid date&lt;br /&gt;
	how_long(&amp;#039;currentdate&amp;#039;)&lt;br /&gt;
	how_long(&amp;#039;29 Feb 2400&amp;#039;)&lt;br /&gt;
	how_long(&amp;#039;29 Feb 2401 BCE&amp;#039;)&lt;br /&gt;
	fridays(2016)&lt;br /&gt;
	next_friday()&lt;br /&gt;
	return table.concat(lines, &amp;#039;\n&amp;#039;)&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
return { main = main }&lt;/div&gt;</summary>
		<author><name>Caleb Cooper</name></author>
	</entry>
</feed>