Difference between revisions of "Os.date"
(4 intermediate revisions by 2 users not shown) | |||
Line 5: | Line 5: | ||
=== Description === | === Description === | ||
Returns a string or a table containing date and time, formatted according to the given string format. | Returns a string or a table containing date and time, formatted according to the given string format. | ||
− | < | + | <syntaxhighlight lang="lua"> |
os.date(format,time) | os.date(format,time) | ||
− | </ | + | </syntaxhighlight> |
− | + | If the ''time'' argument is present, this is the time to be formatted, otherwise, '''date''' formats the current time. | |
− | + | If ''format'' starts with '!', then the date is formatted in Coordinated Universal Time. | |
− | + | After this optional character, if format is the string '''"*t"''', then date returns a table with the following fields: | |
+ | *'''year''' (four digits) | ||
+ | *'''month''' (1--12) | ||
+ | *'''day''' (1--31) | ||
+ | *'''hour''' (0--23) | ||
+ | *'''min''' (0--59) | ||
+ | *'''sec''' (0--59) | ||
+ | *'''wday''' (weekday, Sunday is 1) | ||
+ | *'''yday''' (day of the year) | ||
+ | *'''isdst''' (daylight saving flag, a boolean) | ||
− | When called without arguments, date returns a reasonable date and time representation that depends on the host system and on the current locale (that is | + | If format is not "*t", then date returns the date as a string, formatted according to the same rules as the C function strftime: |
+ | *'''%a''' - Abbreviated weekday name (eg. Wed) | ||
+ | *'''%A''' - Full weekday name (eg. Wednesday) | ||
+ | *'''%b''' - Abbreviated month name (eg. Sep) | ||
+ | *'''%B''' - Full month name (eg. September) | ||
+ | *'''%c''' - Date and time representation appropriate for locale (eg. 23/04/07 10:20:41) | ||
+ | *'''%d''' - Day of month as decimal number (01 - 31) | ||
+ | *'''%H''' - Hour in 24-hour format (00 - 23) | ||
+ | *'''%I''' - Hour in 12-hour format (01 - 12) | ||
+ | *'''%j''' - Day of year as decimal number (001 - 366) | ||
+ | *'''%m''' - Month as decimal number (01 - 12) | ||
+ | *'''%M''' - Minute as decimal number (00 - 59) | ||
+ | *'''%p''' - Current locale’s A.M./P.M. indicator for 12-hour clock (eg. AM/PM) | ||
+ | *'''%S''' - Second as decimal number (00 - 59) | ||
+ | *'''%U''' - Week of year as decimal number, with Sunday as first day of week 1 (00 - 53) | ||
+ | *'''%w''' - Weekday as decimal number (0 - 6; Sunday is 0) | ||
+ | *'''%W''' - Week of year as decimal number, with Monday as first day of week 1 (00 - 53) | ||
+ | *'''%x''' - Date representation for current locale (Standard date string) | ||
+ | *'''%X''' - Time representation for current locale (Standard time string) | ||
+ | *'''%y''' - Year without century, as decimal number (00 - 99) (eg. 07) | ||
+ | *'''%Y''' - Year with century, as decimal number (eg. 2007) | ||
+ | *'''%Z''' - Time-zone name or abbreviation; no characters if time zone is unknown | ||
+ | *'''%%''' - Percent sign | ||
+ | |||
+ | When called without arguments, date returns a reasonable date and time representation that depends on the host system and on the current locale (that is os.date() is equivalent to os.date("%c")). | ||
=== Parameters === | === Parameters === | ||
'''format''': (string) format of the date<br/> | '''format''': (string) format of the date<br/> | ||
'''time''': (number) time to format '''optional'''<br/> | '''time''': (number) time to format '''optional'''<br/> | ||
+ | |||
+ | === Examples === | ||
+ | <syntaxhighlight lang="lua"> | ||
+ | local myt = os.time({year=2003, month=7, day=14, hour=8, min=10, sec=0,}) | ||
+ | local myd = os.date("*t", myt) | ||
+ | print(myd.year, myd.min, myd.sec) | ||
+ | </syntaxhighlight> | ||
+ | |||
+ | <syntaxhighlight lang="lua"> | ||
+ | print(os.date("today is %A, in %B")) -- today is Saturday, in October | ||
+ | print(os.date("%x", 906000490)) -- 09/17/98 | ||
+ | |||
+ | print (os.date ("%x")) --> 25/04/07 | ||
+ | print (os.date ("%c")) --> 25/04/07 10:10:05 | ||
+ | print (os.date ("%A, %m %B %Y")) --> Wednesday, 04 April 2007 | ||
+ | </syntaxhighlight> | ||
+ | |||
+ | === See also === | ||
+ | [[Os.time]] | ||
{{Os}} | {{Os}} |
Latest revision as of 22:48, 14 October 2023
Available since: Gideros 2011.6
Class: os
Description
Returns a string or a table containing date and time, formatted according to the given string format.
os.date(format,time)
If the time argument is present, this is the time to be formatted, otherwise, date formats the current time.
If format starts with '!', then the date is formatted in Coordinated Universal Time.
After this optional character, if format is the string "*t", then date returns a table with the following fields:
- year (four digits)
- month (1--12)
- day (1--31)
- hour (0--23)
- min (0--59)
- sec (0--59)
- wday (weekday, Sunday is 1)
- yday (day of the year)
- isdst (daylight saving flag, a boolean)
If format is not "*t", then date returns the date as a string, formatted according to the same rules as the C function strftime:
- %a - Abbreviated weekday name (eg. Wed)
- %A - Full weekday name (eg. Wednesday)
- %b - Abbreviated month name (eg. Sep)
- %B - Full month name (eg. September)
- %c - Date and time representation appropriate for locale (eg. 23/04/07 10:20:41)
- %d - Day of month as decimal number (01 - 31)
- %H - Hour in 24-hour format (00 - 23)
- %I - Hour in 12-hour format (01 - 12)
- %j - Day of year as decimal number (001 - 366)
- %m - Month as decimal number (01 - 12)
- %M - Minute as decimal number (00 - 59)
- %p - Current locale’s A.M./P.M. indicator for 12-hour clock (eg. AM/PM)
- %S - Second as decimal number (00 - 59)
- %U - Week of year as decimal number, with Sunday as first day of week 1 (00 - 53)
- %w - Weekday as decimal number (0 - 6; Sunday is 0)
- %W - Week of year as decimal number, with Monday as first day of week 1 (00 - 53)
- %x - Date representation for current locale (Standard date string)
- %X - Time representation for current locale (Standard time string)
- %y - Year without century, as decimal number (00 - 99) (eg. 07)
- %Y - Year with century, as decimal number (eg. 2007)
- %Z - Time-zone name or abbreviation; no characters if time zone is unknown
- %% - Percent sign
When called without arguments, date returns a reasonable date and time representation that depends on the host system and on the current locale (that is os.date() is equivalent to os.date("%c")).
Parameters
format: (string) format of the date
time: (number) time to format optional
Examples
local myt = os.time({year=2003, month=7, day=14, hour=8, min=10, sec=0,})
local myd = os.date("*t", myt)
print(myd.year, myd.min, myd.sec)
print(os.date("today is %A, in %B")) -- today is Saturday, in October
print(os.date("%x", 906000490)) -- 09/17/98
print (os.date ("%x")) --> 25/04/07
print (os.date ("%c")) --> 25/04/07 10:10:05
print (os.date ("%A, %m %B %Y")) --> Wednesday, 04 April 2007
See also