没关系,我通过仔细查看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时间戳。希望这可以帮助任何有同样问题的人。
如果要在Web应用程序或节点js应用程序中转换时间,可以轻松使用Moment.Js。 http://momentjs.com