函数:os.date 格式化日期

优质
小牛编辑
121浏览
2023-12-01

函数名称:格式化日期

函数功能:格式化日期

函数方法

str = os.date(format,timeout)

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

格式化字符串

格式符类型类型
%a一星期中天数的简写(Fri)
%A一星期中天数的全称(Wednesday)
%b月份的简写(Sep)
%B月份的全称(May)
%c日期和时间(09/16/98 23:48:10)
%d一个月中的第几天(28)[0 - 31]
%H24小时制中的小时数(18)[00 - 23]
%I12小时制中的小时数(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)
%%字符串'%'(%)
参数类型说明
strstring/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,5000)
--获取今天是星期几
local today = tonumber(os.date("%w",os.time()))
if today ==0 then
    dialog("今天是周日",5000)
elseif  today ==6 then
    dialog("今天是周六",5000)
elseif  today ==1 then
    dialog("今天是周一",5000)
elseif  today ==2 then
    dialog("今天是周二",5000)
elseif  today ==3 then
    dialog("今天是周三",5000)
elseif  today ==4 then
    dialog("今天是周四",5000)
elseif  today ==5 then
    dialog("今天是周五",5000)
end