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

This entry was posted on Fri, 28 Jun 2013 03:11:00 GMT and Posted in . You can follow any any response to this entry through the Atom feed. You can leave a comment or a trackback from your own site.

Tags , ,


Trackbacks

Use the following link to trackback from your own site:
http://wxianfeng.com/trackbacks?article_id=291

Comments

  1. Avatar
    creehack app about {{count}} years ago:

    Having read this I believed it was very informative. I appreciate you taking the
    time and energy to put this short article together.
    I once again find mysepf spending way too much time both reading and posting comments.
    But so what,it was still worthwhile!

  2. Avatar
    creehack app about {{count}} years ago:

    Having read this I believed it was very informative.
    I appreciate you taking the time and energy to pput this short article together.
    I once again find myself spending way too much time both
    reading and posting comments. But so what, it was still worthwhile!

Leave a comment