函数:os.date 格式化日期
函数名称:格式化日期
函数功能:格式化日期
函数方法
str = os.date(format,timeout)
必填
参数 | 类型 | 说明 |
---|---|---|
format | string | 格式化字符串/格式符 |
format 参数介绍
格式符 | 类型 | 类型 |
---|---|---|
%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) |
%% | 字符串'%' | (%) |
选填
参数 | 类型 | 说明 |
---|---|---|
timeout | string | 指定格式化的时间, 不写默认为当前时间 |
返回值
返回值 | 类型 | 说明 |
---|---|---|
str | string/table | 格式化后的时间 |
函数用例
--获取当前日期及时间
local nowTime = os.date("*t",os.time()) --返回一个 table
nLog(nowTime.year) --年
nLog(nowTime.month) --月
nLog(nowTime.day) --日
nLog(nowTime.hour) --小时
nLog(nowTime.min) --分钟
nLog(nowTime.sec) --秒钟
nLog(nowTime.yday) --显示当前为一年中的第几天
--时间戳格式化当前时间
local nowTime = os.date("%Y-%m-%d %H:%M:%S", os.time())
nLog(nowTime)
--获取今天是星期几
local today = tonumber(os.date("%w",os.time()))
if today ==0 then
nLog("今天是周日")
elseif today ==6 then
nLog("今天是周六")
elseif today ==1 then
nLog("今天是周一")
elseif today ==2 then
nLog("今天是周二")
elseif today ==3 then
nLog("今天是周三")
elseif today ==4 then
nLog("今天是周四")
elseif today ==5 then
nLog("今天是周五")
end