Unix Timestamp in Lua

Lua's os library provides Unix timestamp support via os.time(). It returns seconds since the epoch by default. For millisecond precision, the socket library (LuaSocket) or platform-specific C extensions are needed.

Ad

Code Examples

Current timestamp (seconds)

local ts = os.time()

os.time() with no arguments returns the current Unix timestamp in seconds as an integer. This is available in all standard Lua environments.

Current timestamp (milliseconds) — LuaSocket

local socket = require('socket')
local ts_ms = math.floor(socket.gettime() * 1000)

LuaSocket's gettime() returns a floating-point value in seconds with sub-second precision. Multiply by 1000 and floor for milliseconds.

Convert timestamp to date table

local t = os.date('*t', os.time())
print(t.year, t.month, t.day)

os.date('*t', timestamp) returns a table with year, month, day, hour, min, sec fields in local time. Prefix the format with '!' for UTC.

Format timestamp as string

local str = os.date('%Y-%m-%d %H:%M:%S', os.time())

os.date with a format string works like strftime — returns a formatted date string. Prefix with '!' for UTC output.

Convert date table to timestamp

local ts = os.time({year=2024, month=6, day=15, hour=12, min=0, sec=0})

Pass a date table to os.time() to convert a specific date/time to a Unix timestamp. Times are interpreted as local time.

Note

Lua's os.time() returns whole seconds — there is no standard library function for millisecond timestamps. In game engines like Roblox or LÖVE, platform-specific APIs (tick() in Roblox, love.timer.getTime() in LÖVE) provide higher-resolution timing.

Ad

Need to convert a specific timestamp? Use the live converter — paste any epoch value and see the human-readable date instantly.

← Open the converter