函数:os.date 格式化日期

函数名称:格式化日期

函数功能:格式化日期

函数方法

str = os.date(format,timeout)

参数 类型 必填 说明
format string 格式化字符串/格式符
timeout string 指定格式化的时间, 不写默认为当前时间

格式化字符串

格式符 类型 类型
%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)
%% 字符串'%' (%)
返回值 类型 说明
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 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
Copyright 北京帮你玩科技有限公司 2023 all right reserved,powered by Gitbook该文章修订时间: 2024-03-06 17:50:18

results matching ""

    No results matching ""