Lua os.date 笔记

本文详细介绍了Lua语言中os.date函数的使用方法,包括如何格式化日期和时间,获取特定格式的时间戳,以及如何利用*t选项来获取包含各种时间元素的表格。此外,还提供了多个示例来展示如何使用不同的格式化选项。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

参考

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值