Lua os.date 笔记

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

参考

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
`os.date` 是 Lua 标准库中的一个函数,用于格式化日期和时间。它的语法如下: ```lua os.date(format [, time]) ``` 其中,`format` 是格式化字符串,用于指定输出的日期和时间格式,它由以下格式化选项组成: | 格式化选项 | 说明 | | --- | --- | | `%a` | 星期几的缩写(例如:`Mon`) | | `%A` | 星期几的全名(例如:`Monday`) | | `%b` | 月份的缩写(例如:`Jan`) | | `%B` | 月份的全名(例如:`January`) | | `%c` | 日期和时间(例如:`Mon Jan 7 13:23:56 2019`) | | `%d` | 月份中的第几天(例如:`07`) | | `%H` | 小时(24 小时制,例如:`13`) | | `%I` | 小时(12 小时制,例如:`01`) | | `%j` | 年份中的第几天(例如:`007`) | | `%m` | 月份(例如:`01`) | | `%M` | 分钟(例如:`23`) | | `%p` | 上午或下午(例如:`PM`) | | `%S` | 秒(例如:`56`) | | `%w` | 星期几的数字表示(0~6,其中 0 表示星期天) | | `%x` | 日期(例如:`01/07/19`) | | `%X` | 时间(例如:`13:23:56`) | | `%y` | 年份的后两位(例如:`19`) | | `%Y` | 年份的完整表示(例如:`2019`) | | `%z` | 时区偏移量(例如:`-0800`) | `time` 是可选的,用于指定要格式化的日期和时间,它的类型可以是数字或者一个 `os.time()` 返回的时间表。如果不指定 `time`,则默认使用当前的系统时间。 以下是一些示例: ```lua -- 输出当前时间的完整表示 print(os.date("%c")) -- 输出当前时间的日期部分(例如:01/07/19) print(os.date("%x")) -- 输出当前时间的时间部分(例如:13:23:56) print(os.date("%X")) -- 输出当前时间的小时数(例如:13) print(os.date("%H")) -- 输出当前时间的分钟数(例如:23) print(os.date("%M")) -- 输出当前时间的秒数(例如:56) print(os.date("%S")) -- 输出当前时间的星期几的全名(例如:Monday) print(os.date("%A")) -- 输出当前时间的月份的缩写(例如:Jan) print(os.date("%b")) -- 输出当前时间的年份的完整表示(例如:2019) print(os.date("%Y")) ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值