2016-08-14 63 views
0

因此,我想花时间Trello在他们的卡片到期日期中使用它,并将其与Unix时间进行比较,因为我发现当我有两个Unix时间戳时更容易处理。但是,我不确定如何转换它。如何将Trello使用的时间戳转换为unix时间戳?

Trello时间戳是这样的:

2016-08-13T17:27:06.886Z 

第一个数字是日期,和T后的时间。 Z表示它是“祖鲁时间”,与UTC相同。

所以我想要做的就是把它转换成使用Lua的Unix时间戳。

+0

这不是时间戳,这是JSON版本的时间。 –

回答

2

没关系,我发现如何通过仔细观察os.time()函数来做到这一点。

local function formatTime(s) 
    local y = tonumber(string.sub(s, 1, 4)) 
    local m = tonumber(string.sub(s, 6, 7)) 
    local d = tonumber(string.sub(s, 9, 10)) 
    local h = tonumber(string.sub(s, 12, 13)) 
    local mi = tonumber(string.sub(s, 15, 16)) 
    local s = tonumber(string.sub(s, 18, 19)) 

    local tbl = { 
     year = y, 
     month = m, 
     day = d, 
     hour = h, 
     minute = mi, 
     second = s, 
     isdst = (m>=3 and m<=10) --this is roughly close to DST, not perfect. 
    } 

    return os.time(tbl) 
end 

利用这一点,如果我叫了以下内容:

formatTime("2016-08-13T17:27:06.886Z") 

它会返回一个对应于时间Unix时间戳。希望这可以帮助任何有同样问题的人。

-1

如果您想在Web应用程序或节点js应用程序中转换时间,可以轻松使用Moment.Js。 http://momentjs.com

+0

Moment看起来像是一个伟大的Web应用程序工具,但我试图使用Lua。不过谢谢。 – BreadyToCrumble

相关问题