Lua os.date 笔记

Published 05-05-2017 17:32:23

os.date ([format [, time]])

若设置time参数,则按time指定的时间格式化,否则按当前时间格式化

> os.date()
Mon May 16 14:26:32 2016
> os.date("%x", os.time())
05/16/16

如果format 以 ! 开始, date 会被格式化成协调世界时(Coordinated Universal Time)

*t 将返一个带year(4位), month(1-12), day (1–31), hour (0-23), min (0-59), sec (0-61), wday (星期几, 星期天为1), yday (年内天数), isdst (是否为日光节约时间true/false)的表

> print(os.date ("*t"))
table: 0x7fc1aaf01200
> print(os.date ("*t").year)
2017
> print(os.date ("*t").hour)
14
> print(os.date ("*t").wday)
3

若没有*t则返回一个按C的strftime函数格式化的字符串

若不带参数,则按当前系统的设置返回格式化的字符串 os.date() <=> os.date(“%c”)

format 参考

%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)
         (Standard date and time string ) - see below for using os.setlocale to get the correct locale.
%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

示例

获取一年第几周

> os.date ("%V")
12

> os.date ("%Y")
2017

参考