| CARVIEW |
Lua Date and Time module for Lua 5.x.
Jas Latrix
Copyright © 2005-2006 Jas Latrix <jastejada@yahoo.com>
Copyright © 2013-2021 Thijs Schreijer
Table of Contents
To Load the module call local date = require"date" in
your script. Make sure Lua can find the source file date.lua. No global table date
will be created. Use the metamethod __call to construct
a dateObject see example below:
local date = require "date"
-- prints all FRIDAY the 13TH dates between year 2000 and 2010
for i = 2000, 2010 do
-- year jan 1
x = date(i, 1, 1)
-- from january to december
for j = 1, 12 do
-- set date to 13, check if friday
if x:setmonth(j, 13):getweekday() == 6 then
print(x:fmt("%A, %B %d %Y"))
end
end
end
--- OUTPUT ---
--> Friday, October 13 2000
--> Friday, April 13 2001
--> Friday, July 13 2001
--> Friday, September 13 2002
--> Friday, December 13 2002
--> Friday, June 13 2003
--> Friday, February 13 2004
--> Friday, August 13 2004
--> Friday, May 13 2005
--> Friday, January 13 2006
--> Friday, October 13 2006
--> Friday, April 13 2007
--> Friday, July 13 2007
--> Friday, June 13 2008
--> Friday, February 13 2009
--> Friday, March 13 2009
--> Friday, November 13 2009
--> Friday, August 13 2010
- This module does not recognize leap seconds.
- It assumes that a day has exactly
24*60*60seconds. - The Lua number must be a double C data type
- This module supports dates that are greater than
Mon Jan 01 1000000 BCE 00:00:00and less thanMon Jan 01 1000001 00:00:00. - see Local time support for local time limts
Local = UTC + bias
os.date and os.time. It assumes that the Lua function os.time returns the number of seconds
since the start time (called "epoch"). If the time value is outside
the allowable range of times, usually Jan 01 1970
00:00:00 to Jan 19 2038 03:14:07 the bias will
be retrieve using the equivalent year inside the allowable range. Two years are
considered to equivalent if they have the same leap year ness and starting weekday.
The function that needs local time support are date(true) (get the current UTC time), date(false) (get the current local time), date(num_time), dateObj:getbias(), dateObject:fmt(str) (when str has a '%Z' or '%z'),
num_time
or tbl_date or str_date or
bool_now argument describe in the date library __call method.
string, it must be the name of the month
full or abbreviated. If the value is a number, that
number must be 1-12 (January-December). see table below
Table 1.
| Index | Abbreviation | Full Name |
|---|---|---|
| 1 | Jan | January |
| 2 | Feb | February |
| 3 | Mar | March |
| 4 | Apr | April |
| 5 | May | May |
| 6 | Jun | June |
| 7 | Jul | July |
| 8 | Aug | August |
| 9 | Sep | September |
| 10 | Oct | October |
| 11 | Nov | November |
| 12 | Dec | December |
If the value does not represent month, that is equivalent to passing a nil value.
- diff | epoch | isleapyear 3
- Syntax
-
date.diff(
var_date1,var_date2) - Arguments
-
- var_date1
- Required. a dateObject or parsable date value
- var_date2
- Required. a dateObject or parsable date value
- Returns
-
Success The resulting dateObject Failure nil. - Example
-
-- get the days between two dates d = date.diff("Jan 7 1563", date(1563, 1, 2)) assert(d:spandays()==5)
- Syntax
-
date.epoch() - Returns
-
Success The resulting dateObject Failure nil. - Example
-
assert(date.epoch()==date("jan 1 1970"))
- Syntax
-
local centuryflip = date.getcenturyflip() - Arguments
-
- none
- Returns
- The currrent global centuryflip value.
- Remarks
- centuryflip is a global setting and hence should only be set by an application, never by a library.
- Example
-
local centuryflip = date.getcenturyflip()
- Syntax
-
date.isleapyear(
var_year) - Arguments
-
- var_year
- a number or dateObject or parsable date value
- Returns
trueifvar_yearleap year.falseifvar_yearnot leap year.- Remarks
- A leap year in the Gregorian calendar is defined as a year that is evenly divisible by four, except if it is divisible by 100; however, years that are divisible by 400 are leap years.
- Example
-
d = date(1776, 1, 1) assert(date.isleapyear(d)) assert(date.isleapyear(d:getyear())) assert(date.isleapyear(1776))
- Syntax
-
date.setcenturyflip(
century_flip) - Arguments
-
- century_flip
- a number from 0 to 100
- Returns
- Nothing.
- Remarks
- centuryflip is a global setting and hence should only be set by an application, never by a library.
- Example
-
date.setcenturyflip(0) assert(date("01-01-00")==date(1900,01,01)) assert(date("01-01-50")==date(1950,01,01)) assert(date("01-01-99")==date(1999,01,01)) date.setcenturyflip(100) assert(date("01-01-00")==date(2000,01,01)) assert(date("01-01-50")==date(2050,01,01)) assert(date("01-01-99")==date(2099,01,01)) date.setcenturyflip(50) assert(date("01-01-00")==date(2000,01,01)) assert(date("01-01-49")==date(2049,01,01)) assert(date("01-01-50")==date(1950,01,01)) assert(date("01-01-99")==date(1999,01,01))
- __call 1
- 3 or more arguments -
int_year, var_month, int_day, num_hour, num_min, num_sec, int_ticks - 1 argument -
num_timeortbl_dateorstr_dateorbool_now - no argument - same as
date(false)
date:__call(...) is the same as date(...).
- Syntax
-
date(
num_time)date(
tbl_date)date(
str_date)date(
bool_now)date(
int_year,var_month,int_day, [num_hour], [num_min], [num_sec], [int_ticks]) - Arguments
-
- num_time
- Required
numbervalue. Represents the number of seconds in Universal Coordinated Time between the specified value and the System epoch.
- tbl_date
-
Required
tablevalue. The constructor will look for the value of this key:Timeyear- an integer, the full year, for example, 1969. Required if month and day is givenmonth- a parsable month value. Required if year and day is givenday- an integer, the day of month from 1 to 31. Required if year and month is givenhour- a number, hours value, from 0 to 23, indicating the number of hours since midnight. (default = 0)min- a number, minutes value, from 0 to 59. (default = 0)sec- a number, seconds value, from 0 to 59. (default = 0)
(hour or min or sec or msec)must be supplied if date(year and month and day)is not given, vice versa.
- str_date
-
Required
stringvalue. It must have number/words representing date and/or time. Use commas and spaces as delimiters. Strings enclosed by parenthesis is treated as a comment and is ignored, these parentheses may be nested. The stated day of the week is ignored whether its correct or not. A string containing an invalid date is an error. For example, a string containing two years or two months is an error. Time must be supplied if date is not given, vice versa.Time Format. Hours, minutes, and seconds are separated by colons, although all need not be specified. "10:", "10:11", and "10:11:12" are all valid. If the 24-hour clock is used, it is an error to specify "PM" for times later than 12 noon. For example, "23:15 PM" is an error.
Time Zone Format. First character is a sign "+" (east of UTC) or "-" (west of UTC). Hours and minutes offset are separated by colons:
assert( date("Jul 27 2006 03:56:28 +2:00") == date(2006,07,27,1,56,28))Another format is
[sign][number]If[number]is less than 24, it is the offset in hours e.g. "-10" = -10 hours. Otherwise it is the offset in houndred hours e.g. "+75" = "+115" = +1.25 hours.assert(date("Jul 27 2006 -75 ") == date(2006,07,27,1,15,0)) assert(date("Jul 27 2006 -115") == date(2006,07,27,1,15,0)) assert(date("Jul 27 2006 +10 ") == date(2006,07,26,14,0,0)) assert(date("Jul 27 2006 +2 ") == date(2006,07,26,22,0,0))Standard timezone GMT, UTC, EST, EDT, CST, CDT, MST, MDT, PST, PDT are supported.
assert(date("Jul 27 2006 GMT") == date(2006,07,27,0,0,0)) assert(date("Jul 27 2006 UTC") == date(2006,07,27,0,0,0)) assert(date("Jul 27 2006 EST") == date(2006,07,27,5,0,0)) assert(date("Jul 27 2006 EDT") == date(2006,07,27,4,0,0)) assert(date("Jul 27 2006 CST") == date(2006,07,27,6,0,0)) assert(date("Jul 27 2006 CDT") == date(2006,07,27,5,0,0)) assert(date("Jul 27 2006 MST") == date(2006,07,27,7,0,0)) assert(date("Jul 27 2006 MDT") == date(2006,07,27,6,0,0)) assert(date("Jul 27 2006 PST") == date(2006,07,27,8,0,0)) assert(date("Jul 27 2006 PDT") == date(2006,07,27,7,0,0))Date Format. Short dates can use either a "/" or "-" date separator, but must follow the month/day/year format. 2 digit years are interpreted according to the global centuryflip setting.
assert(date("02-03-04")==date(1904,02,03)) assert(date("12/25/98")==date(1998,12,25))Long dates of the form "July 10 1995" can be given with the year, month, and day in any order, and the year in 2-digit or 4-digit form. If you use the 2-digit form, the year must be greater than or equal to 70.
assert(date("Feb-03-04")==date(1904,02,03)) assert(date("December 25 1998")==date(1998,12,25))Follow the year with BC or BCE to indicate that the year is before common era.
assert(date("Feb 3 0003 BC")==date(-2,02,03)) assert(date("December 25 0001 BC")==date(0,12,25))Supported ISO 8601 Formats.
YYYY-MM-DD-
where YYYY is the year, MM is the month of the year, and DD
is the day of the month.
assert(date("2000-12-31")==date(2000,12,31)) assert(date(" 20001231 ")==date(2000,12,31)) -- Compact version YYYY-DDD-
where YYYY is the year, DDD is the day of the year.
assert(date("1995-035")==date(1995,02,04)) assert(date("1995035 ")==date(1995,02,04)) -- Compact version YYYY-WDD-D-
where YYYY is the year, DD is the week of the year, D is the
day of the week.
assert(date("1997-W01-1")==date(1996,12,30)) assert(date(" 1997W017")==date(1997,01,05)) -- Compact version DATE HH:MM:SS.SSS-
Where DATE is the date
format discuss above, HH is the hour, MM is the miute, SS.SSS
is the seconds (fraction is optional).
assert(date("1995-02-04 24:00:51.536")==date(1995,2,5,0,0,51.536)) assert(date("1976-W01-1 12:12:12.123")==date(1975,12,29,12,12,12.123)) assert(date("1995-035 23:59:59.99999")==date(1995,02,04,23,59,59.99999)) -- Compact version separated by latin capital letter T assert(date(" 19950205T000051.536 ")==date(1995,2,5,0,0,51.536)) assert(date(" 1976W011T121212.123 ")==date(1975,12,29,12,12,12.123)) assert(date(" 1995035T235959.99999 ")==date(1995,02,04,23,59,59.99999)) DATE TIME +HH:MM,DATE TIME -HHMM,DATE TIME Z,-
Where DATE and
TIME is the dateand
time format discuss above. First character is a sign "+"
(east of UTC) or "-" (west of UTC). HH and MM is Hours and
minutes offset. The Z stands for the zero offset.
assert(date("1976-W01-1 12:00Z ")==date(1975,12,29,12)) assert(date("1976-W01-1 13:00+01:00")==date(1975,12,29,12)) assert(date("1976-W01-1 0700-0500 ")==date(1975,12,29,12))
- bool_now
- Required
booleanvalue. ifbool_nowisfalseit returns the current local date and time. ifbool_nowistrueit returns the current UTC date and time.
- int_year
- Required
intergervalue. The year value. - var_month
- Required. A parsable month value.
- int_day
- Required
intergervalue. The day of month. - num_hour
- Optional
numbervalue. Equal to the hours value. The default value is0 - num_min
- Optional
numbervalue. Equal to the minutes value. The default value is0 - num_sec
- Optional
numbervalue. Equal to the seconds value. The default value is0 - int_ticks
- Optional
intergervalue. Equal to the ticks value. The default value is0
- Returns
-
Success the new dateObject Failure nil. - Example
-
a = date(2006, 8, 13) assert(a == date("Sun Aug 13 2006")) b = date("Jun 13 1999") assert(b == date(1999, 6, 13)) c = date(1234483200) assert(c == date("Feb 13 2009")) d = date({year=2009, month=11, day=13, min=6}) assert(d == date("Nov 13 2009 00:06:00")) e = date() assert(e)
dateObject as Ticks or Day
Fraction. Date is stored in dateObject as Day
Number. Ticks is time unit per seconds. For example, if the tick unit is 1000000.
0.25 seconds is equivalent to 250000 ticks (0.25*1000000). Day number, is the
number days since the epoch, which is January 1, 0001 AD. Example.
dobj = date("15:49:59.3669")
If the tick unit is 1000000, dobj store this time as
56999366900 ticks and 0 days.
((((15*60)+49)*60)+59.3669)*1000000 == 56999366900Example Date and Time:
dobj = date("Jan-5-0001 10:30:15")
If the tick unit is 1000000, dobj store this date and
time as 37815000000 ticks and 4 days. 4 days becuase:
| Day# | Date |
|---|---|
| 0 | Jan 1 0001 |
| 1 | Jan 2 0001 |
| 2 | Jan 3 0001 |
| 3 | Jan 4 0001 |
| 4 | Jan 5 0001 |
| 5 | Jan 6 0001 |
| ... | ... |
The default tick unit is 1000000 (micro-second-ticks)
- The
a < boperation. - Returns
trueif date value of a is smaller/older than date value of b.a > bis equivalent tob < a. - The
a <= boperation. - Returns
trueif date value of a is smaller/older than or equal to the date value of b.a >= bis equivalent tob <= a. - The
a == boperation. - Returns
trueif date value of a is equal equal to the date value of b.a ~= bis equivalent tonot (a == b). - The
a .. boperation. - Equivalent to
tostring(a) .. tostring(b). - The
a - boperation. - Subtract the date and time value of
bfrom date and time value ofa. - The
a + boperation. - Add the date and time value of
bto date and time value ofa.
a and b must be a
parsable date value or an error rises
a = date(1521,5,2)
b = a:copy():addseconds(0.001)
assert((a - b):spanseconds() == -0.001)
assert((a + b) == (b + a))
assert(a == (b - date("00:00:00.001")) )
assert(b == (a + date("00:00:00.001")) )
b:addseconds(-0.01)
assert(a > b and b < a)
assert(a >= b and b <= a)
assert(a ~= b and (not(a == b)))
a = b:copy()
assert(not (a > b and b < a))
assert(a >= b and b <= a)
assert(a == b and (not(a ~= b)))
assert((a .. 565369) == (b .. 565369))
assert((a .. "????") == (b .. "????"))
- adddays | addhours | addminutes | addmonths | addseconds | addticks | addyears | copy | fmt | getbias | getclockhour | getdate | getday | getfracsec | gethours | getisoweekday | getisoweeknumber | getisoyear | getminutes | getmonth | getseconds | getticks | gettime | getweekday | getweeknumber | getyear | getyearday | setday | sethours | setisoweekday | setisoweeknumber | setisoyear | setminutes | setmonth | setseconds | setticks | setyear | spandays | spanhours | spanminutes | spanseconds | spanticks | tolocal | toutc 44
- Syntax
-
dateObject:adddays(
int_days) - Arguments
-
- int_days
- Required
integervalue. Days to add.
- Returns
-
Success reference to the adjusted dateObject Failure nil. - Remarks
- If the value is negative, the adjusted dateObject will be earlier.
- Example
-
a = date(2000,12,30) b = date(a):adddays(3) c = date.diff(b,a) assert(c:spandays() == 3)
- Syntax
-
dateObject:addhours(
num_hours) - Arguments
-
- num_hours
- Required
numbervalue. Hours to add.
- Returns
-
Success reference to the adjusted dateObject Failure nil. - Remarks
- If the value is negative, the adjusted dateObject will be earlier.
- Example
-
a = date(2000,12,30) b = date(a):addhours(3) c = date.diff(b,a) assert(c:spanhours() == 3)
- Syntax
-
dateObject:addminutes(
num_minutes) - Arguments
-
- num_minutes
- Required
numbervalue. Minutes to add.
- Returns
-
Success reference to the adjusted dateObject Failure nil. - Remarks
- If the value is negative, the adjusted dateObject will be earlier.
- Example
-
a = date(2000,12,30) b = date(a):addminutes(3) c = date.diff(b,a) assert(c:spanminutes() == 3)
- Syntax
-
dateObject:addmonths(
int_months) - Arguments
-
- int_months
- Required
integervalue. Months to add.
- Returns
-
Success reference to the adjusted dateObject Failure nil. - Remarks
- If the value is negative, the adjusted dateObject will be earlier.
- Example
-
a = date(2000,12,28):addmonths(3) assert(a:getmonth() == 3)
- Syntax
-
dateObject:addseconds(
num_sec) - Arguments
-
- num_sec
- Required
numbervalue. Seconds to add.
- Returns
-
Success reference to the adjusted dateObject Failure nil. - Remarks
- If the value is negative, the adjusted dateObject will be earlier.
- Example
-
a = date(2000,12,30) b = date(a):addseconds(3) c = date.diff(b,a) assert(c:spanseconds() == 3)
- Syntax
-
dateObject:addticks(
num_ticks) - Arguments
-
- num_ticks
- Required
numbervalue. Ticks to add.
- Returns
-
Success reference to the adjusted dateObject Failure nil. - Remarks
- If the value is negative, the adjusted dateObject will be earlier. For
discussion about ticks see Section 7.1,
“How Date and Time are stored in
dateObject”. - Example
-
a = date(2000,12,30) b = date(a):addticks(3) c = date.diff(b,a) assert(c:spanticks() == 3)
- Syntax
-
dateObject:addyears(
int_years) - Arguments
-
- int_years
- Required
integervalue. Years to add.
- Returns
-
Success reference to the adjusted dateObject Failure nil. - Remarks
- If the value is negative, the adjusted dateObject will be earlier.
- Example
-
a = date(2000,12,30):addyears(3) assert(a:getyear() == (2000+3))
- Syntax
-
dateObject:copy() - Example
-
a = date(2000,12,30) b = a:copy() assert(a==b)
- Syntax
-
dateObject:fmt(
str_code) - Arguments
-
- str_code
-
stringvalue. The format string follows the same rules as the strftime standard C function.Table 3. Format Spec
Spec Description '%a' Abbreviated weekday name (Sun) '%A' Full weekday name (Sunday) '%b' Abbreviated month name (Dec) '%B' Full month name (December) '%C' Year/100 (19, 20, 30) '%d' The day of the month as a number (range 1 - 31) '%g' year for ISO 8601 week, from 00 (79) '%G' year for ISO 8601 week, from 0000 (1979) '%h' same as %b '%H' hour of the 24-hour day, from 00 (06) '%I' The hour as a number using a 12-hour clock (01 - 12) '%j' The day of the year as a number (001 - 366) '%m' Month of the year, from 01 to 12 '%M' Minutes after the hour 55 '%p' AM/PM indicator (AM) '%S' The second as a number (59, 20 , 01) '%u' ISO 8601 day of the week, to 7 for Sunday (7, 1) '%U' Sunday week of the year, from 00 (48) '%V' ISO 8601 week of the year, from 01 (48) '%w' The day of the week as a decimal, Sunday being 0 '%W' Monday week of the year, from 00 (48) '%y' The year as a number without a century (range 00 to 99) '%Y' Year with century (2000, 1914, 0325, 0001) '%z' Time zone offset, the date object is assumed local time (+1000, -0230) '%Z' Time zone name, the date object is assumed local time '%\b' Year, if year is in BCE, prints the BCE Year representation, otherwise result is similar to "%Y" (1 BCE, 40 BCE) # '%\f' Seconds including fraction (59.998, 01.123) # '%%' percent character % '%r' 12-hour time, from 01:00:00 AM (06:55:15 AM); same as "%I:%M:%S %p" '%R' hour:minute, from 01:00 (06:55); same as "%I:%M" '%T' 24-hour time, from 00:00:00 (06:55:15); same as "%H:%M:%S" '%D' month/day/year from 01/01/00 (12/02/79); same as "%m/%d/%y" '%F' year-month-day (1979-12-02); same as "%Y-%m-%d" '%c' The preferred date and time representation; same as "%x %X" '%x' The preferred date representation, same as "%a %b %d %\b" '%X' The preferred time representation, same as "%H:%M:%\f" '${iso}' Iso format, same as "%Y-%m-%dT%T" '${http}' http format, same as "%a, %d %b %Y %T GMT" '${ctime}' ctime format, same as "%a %b %d %T GMT %Y" '${rfc850}' RFC850 format, same as "%A, %d-%b-%y %T GMT" '${rfc1123}' RFC1123 format, same as "%a, %d %b %Y %T GMT" '${asctime}' asctime format, same as "%a %b %d %T %Y"
- Remarks
- Only English names are supported
- Example
-
d = date(1582,10,5) assert(d:fmt('%D') == d:fmt("%m/%d/%y")) -- month/day/year from 01/01/00 (12/02/79) assert(d:fmt('%F') == d:fmt("%Y-%m-%d")) -- year-month-day (1979-12-02) assert(d:fmt('%h') == d:fmt("%b")) -- same as %b (Dec) assert(d:fmt('%r') == d:fmt("%I:%M:%S %p")) -- 12-hour time, from 01:00:00 AM (06:55:15 AM) assert(d:fmt('%T') == d:fmt("%H:%M:%S")) -- 24-hour time, from 00:00:00 (06:55:15) assert(d:fmt('%a %A %b %B') == "Tue Tuesday Oct October") assert(d:fmt('%C %d') == "15 05", d:fmt('%C %d')) print(d:fmt[[ ${iso} -- iso ${http} -- http ${ctime} -- ctime ${rfc850} -- rfc850 ${rfc1123} -- rfc1123 ${asctime} -- asctime ]]) - Example (Prints the current date and time)
-
-- Prints the current date and time, including time zone d = date(false); print(d:fmt("Today is %c GMT%z")) --> "Today is Tue Oct 31 2000 01:58:14 GMT-0330" -- Prints the current date and time in ISO format including time zone d = date(false); print(d:fmt("Today is ${iso}%z")) --> "Today is 2000-10-31T01:58:14-0330" -- Prints the current date and time in ISO format, indicates UTC d = date(true); print(d:fmt("Today is ${iso}Z")) --> "Today is 2000-10-31T05:28:14Z"
- Syntax
-
dateObject:getbias() - Example
-
a = date(2^16) print(a:getbias())
- Syntax
-
dateObject:getclockhour() - Example
-
a = date("10:59:59 pm") assert(a:getclockhour()==10)
year, month, and day value in a dateObject.
- Syntax
-
dateObject:getdate() - Example
-
a = date(1970, 1, 1) y, m, d = a:getdate() assert(y == 1970 and m == 1 and d == 1)
- Syntax
-
dateObject:getday() - Example
-
d = date(1966, 'sep', 6) assert(d:getday() == 6)
- Syntax
-
dateObject:getfracsec() - Example
-
d = date("Wed Apr 04 2181 11:51:06.996 UTC") assert(d:getfracsec() == 6.996)
- Syntax
-
dateObject:gethours() - Example
-
d = date("Wed Apr 04 2181 11:51:06 UTC") assert(d:gethours() == 11)
- Syntax
-
dateObject:getisoweekday() - Example
-
d = date(1970, 1, 1) assert(d:getisoweekday() == 4)
- Syntax
-
dateObject:getisoweeknumber() - Example
-
d = date(1975, 12, 29) assert(d:getisoweeknumber() == 1) assert(d:getisoyear() == 1976) assert(d:getisoweekday() == 1) d = date(1977, 1, 2) assert(d:getisoweeknumber() == 53) assert(d:getisoyear() == 1976) assert(d:getisoweekday() == 7)
- Syntax
-
dateObject:getisoyear() - Example
-
d = date(1996, 12, 30) assert(d:getisoyear() == 1997) d = date(1997, 01, 05) assert(d:getisoyear() == 1997)
- Syntax
-
dateObject:getminutes() - Example
-
d = date("Wed Apr 04 2181 11:51:06 UTC") assert(d:getminutes() == 51)
- Syntax
-
dateObject:getmonth() - Example
-
d = date(1966, 'sep', 6) assert(d:getmonth() == 9)
- Syntax
-
dateObject:getseconds() - Example
-
d = date("Wed Apr 04 2181 11:51:06.123 UTC") assert(d:getseconds() == 6)
- Syntax
-
dateObject:getticks() - Remarks
- For discussion about ticks see Section 7.1,
“How Date and Time are stored in
dateObject”. - Example
-
d = date("Wed Apr 04 2181 11:51:06.123 UTC") assert(d:getticks() == 123000)
hours, minutes, seconds and ticks value in a dateObject.
- Syntax
-
dateObject:gettime() - Example
-
a = date({hour=5,sec=.5,min=59}) h, m, s, t = a:gettime() assert(t == 500000 and s == 0 and m == 59 and h == 5, tostring(a))
- Syntax
-
dateObject:getweekday() - Example
-
d = date(1970, 1, 1) assert(d:getweekday() == 5)
- Syntax
-
dateObject:getweeknumber([
int_wdaybase]) - Arguments
-
- int_wdaybase
- Optional
integervalue. The starting day of week (1 for sunday, 2 for monday, ... 7 for saturday). If omitted the starting day of week is sunday.
- Example
-
a = date("12/31/1972") b,c = a:getweeknumber(), a:getweeknumber(2) assert(b==53 and c==52)
- Syntax
-
dateObject:getyear() - Example
-
d = date(1965, 'jan', 0) assert(d:getyear() == 1964)
- Syntax
-
dateObject:getyearday() - Example
-
d = date(2181, 1, 12) assert(d:getyearday() == 12)
- Syntax
-
dateObject:setday(
int_mday) - Arguments
-
- int_mday
integervalue. A numeric value equal to the day of month. The default value isthe current day of month
- Returns
-
Success reference to the dateObject Failure nil. - Example
-
d = date(1966, 'july', 6) d:setday(1) assert(d == date("1966 july 1"))
- Syntax
-
dateObject:sethours(
num_hour,num_min,num_sec,num_ticks) - Arguments
-
- num_hour
numbervalue. The hours value. The default value isthe current hours value- num_min
numbervalue. The minutes after the hours value. The default value isthe current minutes value- num_sec
numbervalue. The seconds after the minute value. The default value isthe current seconds value- num_ticks
numbervalue. The ticks after the second value. The default value isthe current ticks value
- Returns
-
Success reference to the dateObject Failure nil. - Example
-
d = date(1984, 12, 3, 4, 39, 54) d:sethours(1, 1, 1) assert(d == date("1984 DEc 3 1:1:1"))
- Syntax
-
dateObject:setisoweekday(
int_wday) - Arguments
-
- int_wday
integervalue. The day the week. The default value isthe current week day
- Returns
-
Success reference to the dateObject Failure nil. - Example
-
d = date.isodate(1999, 52, 1) d:setisoweekday(7) assert(d == date(2000, 1, 02))
- Syntax
-
dateObject:setisoweeknumber(
int_week,int_wday) - Arguments
-
- int_week
integervalue. The week value. The default value isthe current week- int_wday
integervalue. The day of the week. The default value isthe current week day
- Returns
-
Success reference to the dateObject Failure nil. - Example
-
d = date(1999, 12, 27) d:setisoweeknumber(51, 7) assert(d == date(1999, 12, 26))
- Syntax
-
dateObject:setisoyear(
int_year,int_week,int_wday) - Arguments
-
- int_year
integervalue. The year value. The default value isthe current year- int_week
integervalue. The week value. The default value isthe current week- int_wday
integervalue. The day of the week. The default value isthe current week day
- Returns
-
Success reference to the dateObject Failure nil. - Example
-
d = date(1999, 12, 27) d:setisoyear(2000, 1) assert(d == date.isodate(2000,1,1)) assert(d:getyear() == 2000) assert(d:getday() == 3)
- Syntax
-
dateObject:setminutes(
num_min,num_sec,num_ticks) - Arguments
-
- num_min
numbervalue. The minutes after the value. The default value isthe current minutes value- num_sec
numbervalue. The seconds after the minute value. The default value isthe current seconds value- num_ticks
numbervalue. The ticks after the second value. The default value isthe current ticks value
- Returns
-
Success reference to the dateObject Failure nil. - Example
-
d = date(1984, 12, 3, 4, 39, 54) d:setminutes(59, 59, 500) assert(d == date(1984, 12, 3, 4, 59, 59, 500))
- Syntax
-
dateObject:setmonth(
var_month,int_mday) - Arguments
-
- var_month
- parsable month value. The
default value is
the current month - int_mday
integervalue. The day of month. The default value isthe current day of month
- Returns
-
Success reference to the dateObject Failure nil. - Example
-
d = date(1966, 'july', 6) d:setmonth(1) assert(d == date("6 jan 1966"))
- Syntax
-
dateObject:setseconds(
num_sec,num_ticks) - Arguments
-
- num_sec
numbervalue. The seconds after the minute value. The default value isthe current seconds value- num_ticks
numbervalue. The ticks after the second value. The default value isthe current ticks value
- Returns
-
Success reference to the dateObject Failure nil. - Example
-
d = date(1984, 12, 3, 4, 39, 54) d:setseconds(59, date.ticks()) assert(d == date(1984, 12, 3, 4, 40))
- Syntax
-
dateObject:setticks(
num_ticks) - Arguments
-
- num_ticks
numbervalue. The ticks after the second value. The default value isthe current ticks value
- Returns
-
Success reference to the dateObject Failure nil. - Example
-
d = date(1984, 12, 3, 4, 39, 54) d:setticks(444) assert(d == date(1984, 12, 3, 4, 39, 54, 444))
- Syntax
-
dateObject:setyear(
int_year,var_month,int_mday) - Arguments
-
- int_year
integervalue. The year value. The default value isthe current year- var_month
- The month value. The default value is
the current month - int_mday
integervalue. The day of month. The default value isthe current day of month
- Returns
-
Success reference to the dateObject Failure nil. - Example
-
d = date(1966, 'july', 6) d:setyear(2000) assert(d == date("jul 6 2000"))
- Syntax
-
dateObject:spandays() - Example
-
a = date(2181, "aPr", 4, 6, 30, 30, 15000) b = date(a):adddays(2) c = date.diff(b, a) assert(c:spandays() == (2))
- Syntax
-
dateObject:spanhours() - Example
-
a = date(2181, "aPr", 4, 6, 30, 30, 15000) b = date(a):adddays(2) c = date.diff(b, a) assert(c:spanhours() == (2*24))
- Syntax
-
dateObject:spanminutes() - Example
-
a = date(2181, "aPr", 4, 6, 30, 30, 15000) b = date(a):adddays(2) c = date.diff(b, a) assert(c:spanminutes() == (2*24*60))
- Syntax
-
dateObject:spanseconds() - Example
-
a = date(2181, "aPr", 4, 6, 30, 30, 15000) b = date(a):adddays(2) c = date.diff(b, a) assert(c:spanseconds() == (2*24*60*60))
- Syntax
-
dateObject:spanticks() - Remarks
- For discussion about ticks see Section 7.1,
“How Date and Time are stored in
dateObject”. - Example
-
a = date(2181, "aPr", 4, 6, 30, 30, 15000) b = date(a):adddays(2) c = date.diff(b, a) assert(c:spanticks() == (2*24*60*60*1000000))
- Syntax
-
dateObject:tolocal() - Example
-
a = date(2^16) b = a:copy():tolocal(); print(a,b)
- Syntax
-
dateObject:toutc() - Example
-
a = date(2^16) b = a:copy():toutc(); print(a,b)
https://alcor.concordia.ca/~gpkatch/gdate-method.html - Date calculation algorithms is based on this site.