获取年月日等:
--转成年月日接口
function FormatUnixTime2Date(unixTime)
if unixTime and unixTime >= 0 then
local tb = {}
tb.year = tonumber(os.date("%Y",unixTime))
tb.month =tonumber(os.date("%m",unixTime))
tb.day = tonumber(os.date("%d",unixTime))
tb.hour = tonumber(os.date("%H",unixTime))
tb.minute = tonumber(os.date("%M",unixTime))
tb.second = tonumber(os.date("%S",unixTime))
return tb
end
end
--使用方法
local tb = FormatUnixTime2Date(os.time())
--tb.year, tb.month, tb.day, tb.hour, tb.minute, tb.second就是年月日时分秒的值
时间比较:
local from = os.time({year=2020, month=06, day=22, hour=00, min=00, sec=00}) --有些版本是minute/second不是min/sec
local to = os.time({year=2020, month=06, day=22, hour=01, min=00, sec=00})
--如果想要获取当前的时间戳: local nowTime = os.time()
--两个时间点时间的差异,单位秒,可自行转换为其他单位
local diff = to-from
print(diff)
--或者使用os.timediff(),在windows和一般posix系统下与上面等价
local diff = o

本文详细介绍Lua中处理时间的各种方法,包括将Unix时间戳转换为日期格式、比较不同时间点、计算时间差以及进行时间格式的转换。通过实例展示了如何利用Lua内置的os模块和os.date函数来高效操作时间数据。
最低0.47元/天 解锁文章
3539

被折叠的 条评论
为什么被折叠?



