lua源码中os.date的注释以下:git
--- --- Returns a string or a table containing date and time, formatted according --- to the given string `format`. --- --- If the `time` argument is present, this is the time to be formatted (see --- the `os.time` function for a description of this value). 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-61), due to leap seconds --- **`wday`** (weekday, 1–7, Sunday is 1) --- **`yday`** (day of the year, 1–366) --- **`isdst`** (daylight saving flag, a boolean). This last field may be absent --- if the information is not available. --- --- If `format` is not "`*t`", then `date` returns the date as a string, --- formatted according to the same rules as the ISO C function `strftime`. --- --- When called without arguments, `date` returns a reasonable date and time --- representation that depends on the host system and on the current locale. --- (More specifically, `os.date()` is equivalent to `os.date("%c")`.) --- --- On non-POSIX systems, this function may be not thread safe because of its --- reliance on C function `gmtime` and C function `localtime`. ---@overload fun():string|table ---@param format string ---@param time number ---@return string|table function os.date(format, time) end
os.date ([format [, time]])
函数
由原型能够看出能够省略第二个参数也能够省略两个参数,ui
只省略第二个参数函数会使用当前时间做为第二个参数,this
若是两个参数都省略则按当前系统的设置返回格式化的字符串,作如下等价替换 os.date() <=> os.date("%c")。lua
若是format以 “!” 开头,则按格林尼治时间进行格式化。code
若是format是一个 “t” ,将返一个带year(4位),month(1-12), day (1--31), hour (0-23), min (0-59),sec (0-61),wday (星期几, 星期天为1), yday (年内天数)和isdst (是否为日光节约时间true/false)的带键名的表;orm
若是format不是 “t” ,os.date会将日期格式化为一个字符串,具体以下:ip
格式符 | 含义 | 具体示例 |
---|---|---|
%a | 一星期中天数的简写 | os.date("%a") => Fri |
%A | 一星期中天数的全称 | (Wednesday) |
%b | 月份的简写 | (Sep) |
%B | 月份的全称 | (May) |
%c | 日期和时间 | (09/16/98 23:48:10) |
%d | 一个月中的第几天 | (28)[0 - 31] |
%H | 24小时制中的小时数 | (18)[00 - 23] |
%I | 12小时制中的小时数 | (10)[01 - 12] |
%j | 一年中的第几天 | (209) [01 - 366] |
%M | 分钟数 | (48)[00 - 59] |
%m | 月份数 | (09)[01 - 12] |
%P | 上午或下午 | (pm)[am - pm] |
%S | 一分钟以内秒数 | (10)[00 - 59] |
%w | 一星期中的第几天 | (3)[0 - 6 = 星期天 - 星期六] |
%W | 一年中的第几个星期 | (2)0 - 52 |
%x | 日期 | (09/16/98) |
%X | 时间 | (23:48:10) |
%y | 两位数的年份 | (16)[00 - 99] |
%Y | 完整的年份 | (2016) |
%% | 字符串'%' | (%) |
*t | 返回一个table,里面包含所有的数据 | hour 14 |
个人lua版本:lua5.3ci
print ("os.date(\"*t\") 示例:\n") local timetable = os.date("*t", os.time()); for i, v in pairs(timetable) do print(i, v); end print ("\n \"!\" 开头:\n") local utimetable = os.date("!*t", os.time()); for i, v in pairs(utimetable) do print(i, v); end print ("\n其它用法:\n") print(os.date("今天是 %c, 星期 %A"))
输出结果:字符串
今天东八区中国广州的日期为:2018-11-1 21:13 星期四
os.date("*t") 示例: wday 5 year 2018 day 1 sec 51 hour 21 isdst false month 11 yday 305 min 13
os.date("!*t") 示例: wday 5 year 2018 day 1 sec 38 hour 13 --- 差8小时 isdst false month 11 yday 305 min 14
今天是 11/01/18 21:15:36, 星期 Thursday