Current Unix Timestamp SECONDS SINCE JAN 01 1970

Current UTC Time
2025-08-20
Convert Unix Timestamp to Datetime in Lua

How to convert Unix timestamp to Datetime in Lua

🎈 🎈 🎈
+1

    -- Convert Unix timestamp to Date
    local unixTimestamp = 1512989095
    local date = os.date("%Y-%m-%d %H:%M:%S", unixTimestamp)
    
    print("Converted Date: " .. date)            
        

Output:
Converted Date: 2017-12-11 10:44:55
Example only. There may be multiple ways to perform this operation.

Code Explanation

In this Unix timestamp to date conversion in Lua, the Unix timestamp 1512989095 is passed to the os.date function along with the format string %Y-%m-%d %H:%M:%S, which formats the timestamp into a readable date.
The result is the date 2017-12-11 10:44:55, which is printed. In terms of the formatting, the %Y represents the year, %m represents the month, %d represents the day, %H is the hour, %M is the minute, and %S is the second.



Other useful Lua date functions

Function Description Example
os.date() Returns the formatted date and time as a string. Can take a format string and a Unix timestamp. os.date("%Y-%m-%d %H:%M:%S", 1589530245) returns 2020-05-15 08:10:45
os.time() Returns the current time as a Unix timestamp or converts a table with date components to a Unix timestamp. os.time({year=2020, month=5, day=15, hour=8, min=30, sec=45}) returns 1589530245
os.clock() Returns the CPU time used by the program in seconds. os.clock() returns the CPU time used by the script.
os.difftime() Returns the difference in seconds between two Unix timestamps. os.difftime(1589530245, 1589443845) returns 86400 (1 day).
os.setlocale() Sets the locale for date and time formatting. os.setlocale("en_US.UTF-8") sets the locale to US English.
os.date("*t") Returns the current date and time as a table with keys for the year, month, day, hour, etc. os.date("*t") returns a table representing the current date and time.
os.date("!*t") Returns the UTC date and time as a table with keys for the year, month, day, hour, etc. os.date("!*t") returns a table representing the UTC date and time.
os.time() Returns the current Unix timestamp. os.time() returns the current Unix timestamp.
Current Unix Timestamp SECONDS SINCE JAN 01 1970

Current UTC Time
2025-08-20
Ad Banner Placeholder
Ad Banner Placeholder
Ad Banner Placeholder
Ad Banner Placeholder