Compare dates in Lua

comparison, date, lua

Solution

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"

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.

Problem

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!

Original source