比较卢阿的日期 [英] Compare dates in Lua

查看:73
本文介绍了比较卢阿的日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有日期表的变量

I have a variable with a date table that looks like this

* table:
 [day]
  * number: 15
 [year]
  * number: 2015
 [month]
  * number: 2

如何获取当前日期和上述日期之间的日期?非常感谢!

How do I get the days between the current date and the date above? Many thanks!

推荐答案

您可以使用 os.time()来将您的表转换为秒并获取当前时间,然后使用 os.difftime()计算差异。有关更多详细信息,请参见 Lua Wiki

You can use os.time() to convert your table to seconds and get the current time and then use os.difftime() to compute the difference. see Lua Wiki for more details.

reference = os.time{day=15, year=2015, month=2}
daysfrom = os.difftime(os.time(), reference) / (24 * 60 * 60) -- seconds in a day
wholedays = math.floor(daysfrom)
print(wholedays) -- today it prints "1"

正如@ barnes53指出的那样,一天之内可能会关闭几秒钟,所以并不理想,但这可能足以满足您的需求。

as @barnes53 pointed out could be off by one day for a few seconds so it's not ideal, but it may be good enough for your needs.

这篇关于比较卢阿的日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆