获取年月日等:
--转成年月日接口
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 = os.difftime(to,from)
print(diff)
时间格式转换:
local datatime1 = os.date("%Y%m%d%H%M%S"); --20200622000000
local datatime2 = os.date("%Y-%m-%d %H:%M:%S"); --2020-06-22 00:00:00
local datatime3 = os.date("%Y%m%d%H%M%S", from); --20200622000000 from是上面的os.time