Get time and date

Verified
Added by iNTERFACEWARE

Using date and time functions with some formatting examples

Source Code
   -- get the current time   
   -- in a custom userdata format
   local tm = os.time()   
   -- as a Unix Epoch time
   local tm = os.ts.time()
   
   -- format a supplied time
   -- in a userdata format
   local tm = os.time{year=1999, month=12, day=01}
   -- hour, min & sec are optional
   local tm = os.time{year=1999, month=12, day=1, hour=22, min=23, sec=24}  
   -- as a Unix Epoch time
   local tm = os.ts.time{year=1999, month=12, day=01}
   -- hour, min & sec are optional
   local tm = os.ts.time{year=1999, month=12, day=1, hour=22, min=23, sec=24}  

      
   -- get the current date
   -- as a string (all 4 return the same)
   local tm = os.date()
   local tm = os.ts.date()   
   local tm = os.date('%c')
   local tm = os.ts.date('%c')
   -- as a table
   local tm = os.date('*t')
   local tm = os.ts.date('*t')
   
   
   -- format a supplied date
   -- as a string (from a userdata format)
   local tm = os.date('%c',os.time{year=1999, month=12, day=1})
   local tm = os.date(_,os.time{year=1999, month=12, day=1})
   -- as a string from a Unix Epoch time
   local tm = os.ts.date('%c',os.ts.time{year=1999, month=12, day=1})
   local tm = os.ts.date(_,os.ts.time{year=1999, month=12, day=1})
   -- as a table
   local tm = os.date('*t',os.time{year=1999, month=12, day=1})
   local tm = os.ts.date('*t',os.ts.time{year=1999, month=12, day=1})
   -- date in mm/dd/yy format
   local tm = os.date('%x',os.time{year=1999, month=12, day=1})
   local tm = os.ts.date('%x',os.ts.time{year=1999, month=12, day=1})
   -- 24 hour time
   local tm = os.date('%X',os.time{year=1999, month=12, day=1, hour=22, min=23, sec=24})
   local tm = os.ts.date('%X',os.ts.time{year=1999, month=12, day=1, hour=22, min=23, sec=24})
   -- date and time
   local tm = os.date('%x %X',os.time{year=1999, month=12, day=1, hour=22, min=23, sec=24})
   local tm = os.ts.date('%x %X',os.ts.time{year=1999, month=12, day=1, hour=22, min=23, sec=24})
Description
Using date and time functions with some formatting examples
Usage Details

Iguana has the standard Lua functions os.ts.time() and os.ts.date(), as well as our own versions implemented using with custom userdata date/time. This article from Eliot explains our history of time functions.

How to use the snippet:

  • Paste the desired time/date code your script
  • Modify it to suit your needs

Tip: Using the fuzzy date time module can be really helpful.