Lua 时间处理

Posted by wxianfeng Fri, 28 Jun 2013 03:11:00 GMT

得到当前时间

> print(os.date())
Wed Jun 26 10:27:15 2013

得到当前时间戳

> print(os.time())
1372155588

时间戳转时间

> print(os.date("%c",1372153800))
Tue Jun 25 17:50:00 2013

时间转时间戳

-- Assuming a date pattern like: yyyy-mm-dd hh:mm:ss
local pattern = "(%d+)-(%d+)-(%d+) (%d+):(%d+):(%d+)"
local timeToConvert = "2011-01-01 01:30:33"
local runyear, runmonth, runday, runhour, runminute, runseconds = timeToConvert:match(pattern)

local convertedTimestamp = os.time({year = runyear, month = runmonth, day = runday, hour = runhour, min = runminute, sec = runseconds})

计算 30 分钟前时间, 可以使用 秒计算出

> print(os.time() - 30*60)
1372153800
> print(os.date("%c",1372153800))
Tue Jun 25 17:50:00 2013