A Date Time Function

Send Snippet To: Save this snippet to Code Collector Pro -- view all roger's snippets
language: Other
license: Other

Code for Snippet:

                
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"
comments powered by Disqus

Info

Link to this snippet:


Download to Code Collector

To use the direct link to your snippet on CodeCollector.net either copy the html from the above section or drag the Download to Code Collector to where you would like to use it.

More Info:

Times Viewed: 5
Date Added: 2018-01-09 04:55:13
Last Modified: 2018-01-10 09:54:13

Web Analytics