函数:os.date 格式化日期
函数名称:格式化日期
函数功能:格式化日期
函数方法
str = os.date(format,timeout)
参数 | 类型 | 必填 | 说明 |
---|---|---|---|
format | string | 是 | 格式化字符串/格式符 |
timeout | string | 否 | 指定格式化的时间, 不写默认为当前时间 |
格式化字符串
格式符 | 介绍 | 返回值内容 | 返回值示例 |
---|---|---|---|
%a | 一星期中天数的简写,周一至周日 | Mon ~ Sun | Mon |
%A | 一星期中天数的全称,周一至周日 | Monday ~ Sunday | Monday |
%b | 月份的简写 | Jan ~ Dec | Apr |
%B | 月份的全称 | January ~ December | Apri |
%c | 日期和时间 | 具体时间日期精确到秒 | Mon Apr 28 12:54:02 2025 |
%d | 一个月中的第几天 | 0 ~ 31 | 28 |
%H | 24 小时制中的小时数 | 00 ~ 23 | 12 |
%I | 12 小时制中的小时数 | 00 ~ 12 | 12 |
%j | 一年中的第几天 | 01~366 | 209 |
%M | 分钟数 | 00 ~ 59 | 48 |
%m | 月份数 | 01 ~ 12 | 09 |
%P | 上午或下午 | am - pm | am |
%S | 一分钟之内秒数 | 00 - 59 | 02 |
%w | 一星期中的第几天 | 0 - 6,表示星期天 - 星期六 | 1 |
%W | 一年中的第几个星期 | 0 - 52 | 2 |
%x | 日期 | 具体年月日 | 04/28/25 |
%X | 时间 | 具体时分秒 | 12:54:02 |
%y | 两位数的年份 | 00 - 99 | 25 |
%Y | 完整的年份 | 年份 | 2025 |
%% | 字符串'%' | - | (%) |
返回值 | 类型 | 说明 |
---|---|---|
str | string/table | 格式化后的时间 |
函数用例
--获取当前日期及时间
local nowTime = os.date("*t",os.time()) --返回一个 table
dialog(nowTime.year,5) --年
dialog(nowTime.month,5) --月
dialog(nowTime.day,5) --日
dialog(nowTime.hour,5) --小时
dialog(nowTime.min,5) --分钟
dialog(nowTime.sec,5) --秒钟
dialog(nowTime.yday,5) --显示当前为一年中的第几天
--时间戳格式化当前时间
local nowTime = os.date("%Y-%m-%d %H:%M:%S", os.time())
dialog(nowTime,5)
--根据当前时间戳获取当前小时
local time = os.time()
local hour = os.date("*t", time).hour
dialog(hour,5)
--获取今天是星期几
local today = tonumber(os.date("%w",os.time()))
if today ==0 then
dialog("今天是周日",5)
elseif today ==6 then
dialog("今天是周六",5)
elseif today ==1 then
dialog("今天是周一",5)
elseif today ==2 then
dialog("今天是周二",5)
elseif today ==3 then
dialog("今天是周三",5)
elseif today ==4 then
dialog("今天是周四",5)
elseif today ==5 then
dialog("今天是周五",5)
end