Dealing With Date Time

lua-users home
wiki

You can use os.time() to add or substract any number of any time unit (e.g. 10000 minutes or -3 months) without converting it to seconds. What is even better, that it deals with daylight savings too.

This feature of os.date() and os.time() is not documented on www.lua.org [1], resulting a large amount of questions on Stackoverflow[2].

-- no surprise here:
os.date("%c", os.time{year = 2017, month = 01, day = 01, hour = 0, min = 0, sec = 0})
>> "Sun Jan  1 00:00:00 2017"

-- and the undocumented feature:
os.date("%c", os.time{year = 2017, month = 01, day = 01, hour = 0, min = 20000, sec = 0})
>> "Sat Jan 14 21:20:00 2017"

-- also works with negative numbers:
os.date("%c", os.time{year = 2017, month = 01, day = -20, hour = 0, min = 0, sec = 0})
>> "Sun Dec 11 00:00:00 2016"


RecentChanges · preferences
edit · history
Last edited July 11, 2017 11:33 am GMT (diff)