Answered by
6 years ago Edited 6 years ago
Using os.time()
is pretty straight forward, like it says in the wiki:
Returns how many seconds since the epoch (1 January 1970, 00:00:00), under UTC time.
Example usage:
1 | print ( "It has been " .. os.time() .. " seconds since the epoch." ) |
os.time()
also has an optional parameter. You can input a dateTable ( essentially a date and time in the form of a table, will get to more on it later ) to get how many seconds since the epoch up until that date.
os.date()
is a little different and much more useful. It has two parameters. The first is the format string which determines if you want the date in local time or in UTC by inputting either "*t"
or "!*t"
respectively. The second parameter is the number of seconds from epoch. This is where os.time()
is useful. If you want to get the current dateTable, you can use os.time()
as the second input.
This function returns a dateTable
, which is actually just a table but organized in a specific way.
Here is an example usage of os.date()
as well as the format of the dateTable
it returns.
01 | local dateTable = os.date( "*t" , os.time()) |
17 | local days = { "Sunday" , "Monday" , "Tuesday" , "Wednesday" , "Thursday" , "Friday" , "Saturday" } |
20 | local message = string.format( "Today is %s and it is d:d right now." , days [ dateTable.wday ] , dateTable.hour, dateTable.min) |
I hope this gives you an understanding of os.time()
and os.date()
!