Date and Time functions

Introduction

This page presents sample code for retrieving time and date information using the built-in date and time functions and also shows you how to format it in various ways. You can just copy whatever you need directly into your project and modify it to match your requirements.

Also you will probably want to take a look at the fuzzy date/time parser (in the Iguana Date/Time repository) which translates a wide variety of date/time formats. The dateparse module automatically translates a wide variety of date/time formats, but it can’t predict everything, so you can also create custom date formats.

If you have any questions please contact us at support@interfaceware.com.

Code for Date and Time functions [top]

 -- 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 or 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})

Using Date and Time functions [top]

The code demonstrates how to use and format date/time functions.

How to use the code:

  1. Paste the desired code into any module as required.
  2. Then modify the code to suit your requirements.

More Information [top]

Tagged:

Leave A Comment?

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.