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, date1 <= date2
), and can be used with add or subtract (for example, date + '3 months'
). The difference between two dates can be determined with date1 - date2
. These operations work with both Gregorian and Julian calendar dates, but date1 - date2
is nil if the two dates use different calendars.
The module provides the following items.
Export | Description | |
---|---|---|
_current | Table with the current year, month, day, hour, minute, second. | |
_Date | Function that returns a table for a specified date. | |
_days_in_month | Function that returns the number of days in a month. |
The following has examples of using the module:
A date can be formatted as text.
The following simplified formatting codes are available.
Code | Result | |
---|---|---|
hm | hour:minute, with "am" or "pm" or variant, if specified (14:30 or 2:30 pm or variant) | |
hms | hour:minute:second (14:30:45) | |
ymd | year-month-day (2016-07-01) | |
mdy | month day, year (July 1, 2016) | |
dmy | day month year (1 July 2016) |
The following formatting codes (similar to strftime) are available.
Code | Result | |
---|---|---|
%a | Day abbreviation: Mon, Tue, ... | |
%A | Day name: Monday, Tuesday, ... | |
%u | Day of week: 1 to 7 (Monday to Sunday) | |
%w | Day of week: 0 to 6 (Sunday to Saturday) | |
%d | Day of month zero-padded: 01 to 31 | |
%b | Month abbreviation: Jan to Dec | |
%B | Month name: January to December | |
%m | Month zero-padded: 01 to 12 | |
%Y | Year zero-padded: 0012, 0120, 1200 | |
%H | Hour 24-hour clock zero-padded: 00 to 23 | |
%I | Hour 12-hour clock zero-padded: 01 to 12 | |
%p | AM or PM or as in options | |
%M | Minute zero-padded: 00 to 59 | |
%S | Second zero-padded: 00 to 59 | |
%j | Day of year zero-padded: 001 to 366 | |
%-d | Day of month: 1 to 31 | |
%-m | Month: 1 to 12 | |
%-Y | Year: 12, 120, 1200 | |
%-H | Hour: 0 to 23 | |
%-M | Minute: 0 to 59 | |
%-S | Second: 0 to 59 | |
%-j | Day of year: 1 to 366 | |
%-I | Hour: 1 to 12 | |
%% | % |
In addition, %{''property''}
(where ''property''
is any property of a date) can be used.
For example, Date('1 Feb 2015 14:30:45 A.D.')
has the following properties.
Code | Result | |
---|---|---|
% | Gregorian | |
% | 2015 | |
% | 2 | |
% | 1 | |
% | 14 | |
% | 30 | |
% | 45 | |
% | Sun | |
% | Sunday | |
% | 0 | |
% | 0 (same as dayofweek) | |
% | 7 | |
% | 7 (same as dayofweekiso) | |
% | 32 | |
% | A.D. | |
% | 735630 (numbers of days from 1 January 1 CE; the first is day 1) | |
% | 2457055.1046875 (Julian day) | |
% | 2457055.1046875 (same as juliandate) | |
% | false | |
% | 28 | |
% | Feb | |
% | February |
Some shortcuts are available. Given date = Date('1 Feb 2015 14:30')
, the following results would occur.
Code | Description | Example result | Equivalent format | |
---|---|---|---|---|
date:text('%c') | date and time | 2:30 pm 1 February 2015 | %-I:%M %p %-d %B %-Y % | |
date:text('%x') | date | 1 February 2015 | %-d %B %-Y % | |
date:text('%X') | time | 2:30 pm | %-I:%M %p |
The following has an example of converting a Julian date to a date, then obtaining information about the date.
The difference between two dates can be determined with date1 - date2
. 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:
A date difference holds the original dates except they are swapped so diff.date1 >= diff.date2
(diff.date1
is the more recent date). This is shown in the following.
A date difference also holds a time difference:
A date difference can be added to a date, or subtracted from a date.
date4) -- true
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.
Code | Returned values | |
---|---|---|
'ymwd' | years, months, weeks, days | |
'ymd' | years, months, days | |
'ym' | years, months | |
'y' | years | |
'm' | months | |
'wd' | weeks, days | |
'w' | weeks | |
'd' | days |