Module:Date/doc

This module provides date functions for use by other modules. Dates in the Gregorian calendar and the Julian calendar are supported, from 9999 BCE to 9999 CE. The calendars are proleptic—they are assumed to apply at all times with no irregularities.

A date, with an optional time, can be specified in a variety of formats, and can be converted for display using a variety of formats, for example, 1 April 2016 or April 1, 2016. The properties of a date include its Julian date and its Gregorian serial date, as well as the day-of-week and day-of-year.

Dates can be compared (for example, &#x3c;code&#x3e;date1 &#x3c;= date2&#x3c;/code&#x3e;), and can be used with add or subtract (for example, &#x3c;code&#x3e;date + '3 months'&#x3c;/code&#x3e;). The difference between two dates can be determined with &#x3c;code&#x3e;date1 - date2&#x3c;/code&#x3e;. These operations work with both Gregorian and Julian calendar dates, but &#x3c;code&#x3e;date1 - date2&#x3c;/code&#x3e; is nil if the two dates use different calendars.

The module provides the following items.

The following has examples of using the module:
 * Module:Date/example • Demonstration showing how Module:Date may be used.
 * Module talk:Date/example • Output from the demonstration.

Formatted output
A date can be formatted as text. &#x3c;syntaxhighlight lang="lua"&#x3e; local Date = require('Module:Date')._Date local text = Date(2016, 7, 1):text         -- result is '1 July 2016' local text = Date(2016, 7, 1):text('%-d %B') -- result is '1 July' local text = Date('1 July 2016'):text('mdy') -- result is 'July 1, 2016' &#x3c;/syntaxhighlight&#x3e;

The following simplified formatting codes are available.

The following formatting codes (similar to strftime) are available.

In addition, &#x3c;code&#x3e;%{property}&#x3c;/code&#x3e; (where &#x3c;code&#x3e;property&#x3c;/code&#x3e; is any property of a date) can be used.

For example, &#x3c;code&#x3e;Date('1 Feb 2015 14:30:45 A.D.')&#x3c;/code&#x3e; has the following properties.

Some shortcuts are available. Given &#x3c;code&#x3e;date = Date('1 Feb 2015 14:30')&#x3c;/code&#x3e;, the following results would occur.

Julian date
The following has an example of converting a Julian date to a date, then obtaining information about the date. &#x3c;syntaxhighlight lang="lua"&#x3e; -- Code                                               -- Result Date = require('Module:Date')._Date date = Date('juliandate', 320) number = date.gsd                                     -- -1721105 number = date.jd                                      -- 320 text = date.dayname                                   -- Saturday text = date:text                                    -- 9 October 4713&#x26;nbsp;BC text = date:text('%Y-%m-%d')                          -- 4713-10-09 text = date:text('%{era} %Y-%m-%d')                   -- BC 4713-10-09 text = date:text('%Y-%m-%d %{era}')                   -- 4713-10-09&#x26;nbsp;BC text = date:text('%Y-%m-%d %{era}', 'era=B.C.E.')     -- 4713-10-09&#x26;nbsp;B.C.E. text = date:text('%Y-%m-%d', 'era=BCNEGATIVE')         -- -4712-10-09 text = date:text('%Y-%m-%d', 'era=BCMINUS')           -- −4712-10-09 (uses Unicode MINUS SIGN U+2212) text = Date('juliandate',320):text('%{gsd} %{jd}')    -- -1721105 320 text = Date('Oct 9, 4713 B.C.E.'):text('%{gsd} %{jd}') -- -1721105 320 text = Date(-4712,10,9):text('%{gsd} %{jd}')          -- -1721105 320 &#x3c;/syntaxhighlight&#x3e;

Date differences
The difference between two dates can be determined with &#x3c;code&#x3e;date1 - date2&#x3c;/code&#x3e;. The result is valid if both dates use the Gregorian calendar or if both dates use the Julian calendar, otherwise the result is nil. An age and duration can be calculated from a date difference.

For example: &#x3c;syntaxhighlight lang="lua"&#x3e; -- Code                                     -- Result Date = require('Module:Date')._Date date1 = Date('21 Mar 2015') date2 = Date('4 Dec 1999') diff = date1 - date2 d = diff.age_days                           -- 5586 y, m, d = diff.years, diff.months, diff.days -- 15, 3, 17 (15 years + 3 months + 17 days) y, m, d = diff:age('ymd')                   -- 15, 3, 17 y, m, w, d = diff:age('ymwd')               -- 15, 3, 2, 3 (15 years + 3 months + 2 weeks + 3 days) y, m, w, d = diff:duration('ymwd')          -- 15, 3, 2, 4 d = diff:duration('d')                      -- 5587 (a duration includes the final day) &#x3c;/syntaxhighlight&#x3e;

A date difference holds the original dates except they are swapped so &#x3c;code&#x3e;diff.date1 &#x3e;= diff.date2&#x3c;/code&#x3e; (&#x3c;code&#x3e;diff.date1&#x3c;/code&#x3e; is the more recent date). This is shown in the following. &#x3c;syntaxhighlight lang="lua"&#x3e; date1 = Date('21 Mar 2015') date2 = Date('4 Dec 1999') diff = date1 - date2 neg = diff.isnegative                       -- false text = diff.date1:text                    -- 21 March 2015 text = diff.date2:text                    -- 4 December 1999 diff = date2 - date1 neg = diff.isnegative                       -- true (dates have been swapped) text = diff.date1:text                    -- 21 March 2015 text = diff.date2:text                    -- 4 December 1999 &#x3c;/syntaxhighlight&#x3e;

A date difference also holds a time difference: &#x3c;syntaxhighlight lang="lua"&#x3e; date1 = Date('8 Mar 2016 0:30:45') date2 = Date('19 Jan 2014 22:55') diff = date1 - date2 y, m, d = diff.years, diff.months, diff.days     -- 2, 1, 17 H, M, S = diff.hours, diff.minutes, diff.seconds -- 1, 35, 45 &#x3c;/syntaxhighlight&#x3e;

A date difference can be added to a date, or subtracted from a date. &#x3c;syntaxhighlight lang="lua"&#x3e; date1 = Date('8 Mar 2016 0:30:45') date2 = Date('19 Jan 2014 22:55') diff = date1 - date2 date3 = date2 + diff date4 = date1 - diff text = date3:text('ymd hms')       -- 2016-03-08 00:30:45 text = date4:text('ymd hms')       -- 2014-01-19 22:55:00 equal = (date1 == date3)           -- true equal = (date2 == date4)           -- true &#x3c;/syntaxhighlight&#x3e;

The age and duration methods of a date difference accept a code that identifies the components that should be returned. An extra day is included for the duration method because it includes the final day.