如何将Trello使用的时间戳转换成Unix时间戳?

所以,我正在尝试使用Trello卡片到期日期中使用的时间与Unix时间进行比较,因为当我有两个Unix时间戳时,我发现处理起来更容易。但是,我不确定如何进行转换。

Trello时间戳的格式如下:

2016-08-13T17:27:06.886Z

前面的数字是日期,T之后是时间。Z表示它是“Zulu时间”,与UTC相同。

所以我想做的是使用Lua将其转换为Unix时间戳。

点赞
用户3047810
用户3047810

如果你想在一个网页应用或者 Node.js 应用中转换时间,你可以很容易地使用 Moment.Js。 http://momentjs.com

2016-08-14 02:55:32
用户6713882
用户6713882

没事了,我通过仔细查看 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) --这大概靠近夏令时,不太准确。
    }

    return os.time(tbl)
end

使用上述函数,如果我调用以下代码:

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

它将返回对应于那个时间的 Unix 时间戳。希望这能帮助那些有同样问题的人。

2016-08-14 03:58:04