How to get the current date and time?

os.date without any arguments will give you a system-defined representation of the current date and time. You can pass it a formatting string just as the C function strftime:

> = os.date()
07/23/09 15:18:49
> = os.date '%A'
Thursday
> = os.date '%b'
Jul
> = os.date '%B'
July
> = os.date '%C'
> = os.date '%a %b %d %X %Y'
Thu Jul 23 15:21:38 2009

Note: the Microsoft implementation of strftime does not handle the full set of formatting specifiers. It is probably best, for cross-platform code, to stick to the codes specified here (some specifiers valid on Unix will actually cause Lua to crash on Windows - Lua 5.2 makes a sanity check here.):

The second, optional argument is the time to be formatted. This is the value returned by os.time and is (usually) the number of seconds since the start of the epoch, which is 1 Jan 1970.

os.time can be passed a table to be converted into a time value. See the manual for details.



Back