Lua:将Date表更改为字符串或其他格式

时间:2014-11-16 18:31:01

标签: date datetime time lua

我查看了许多页面,告诉我日期和时间在lua中的工作方式,但还没有找到解决方案。 基本上我想要获取当前日期和时间,然后在该日期添加1个月,然后将该新日期保存到我的MySQL表中,格式可以在以后与当天的当天比较。像这样:if now > oldDate then do something

我设法将当前日期/时间作为一个表格,并通过这样做添加1个月:

local t = os.date( "*t" )
t.month = t.month + 1

但我无法找到如何将其转换为某种DateTime格式,我可以将其存储到MySQL然后进行比较。

任何帮助将不胜感激!

1 个答案:

答案 0 :(得分:6)

Lua的os.date接受一个可选的时间参数作为第二个参数

> local t = os.date("*t")
> t.month = t.month + 1
> print(os.date("%Y-%m-%d", os.time(t)) 
2014-12-16

然后可以将该字符串作为日期插入MySQL表中。

要比较Lua中的两个日期,请比较os.time()

的输出
if os.time() > os.time(t) then
    print("The time has come.")
end