For Developers

Unix Timestamp in Lua

How to get the current timestamp, convert it to a date, and convert a date back to a timestamp in Lua — the version used by both stock Lua and, with caveats, LuaJIT/embedded runtimes like Redis and game engines.

local ts = os.time()
local ts = 1750000000
local t = os.date("!*t", ts) -- "!" forces UTC instead of local time
print(t.year, t.month, t.day, t.hour, t.min, t.sec)
local ts = os.time({year=2025, month=6, day=15, hour=12, min=0, sec=0})
-- Note: os.time() with a table always interprets it as LOCAL time; there is
-- no built-in way to build a UTC timestamp from a table without a workaround.

The Lua-specific pitfall

Lua's standard library has no built-in way to construct a Unix timestamp from a UTC date table — os.time() always assumes the table is local time, even though os.date("!*t") can read a timestamp back out in UTC. The common workaround is computing the local machine's current UTC offset (by comparing os.time() against os.date("!*t") and os.date("*t") for the same instant) and adjusting manually. This asymmetry — easy to read UTC, hard to write it — trips up anyone porting date logic from a language where both directions are equally supported. It's also worth knowing that embedded Lua runtimes (game engines, Redis scripting, OpenResty) sometimes strip down or sandbox the os library entirely, so os.time() may not even be available depending on where the script runs.

Have a timestamp right now?

Paste it into the Timestamp ⇄ Date Converter to check it instantly, in any timezone, without writing any code. Working across several languages on the same project? The full Epoch Time in Programming Languages guide has all of them side by side for quick comparison.

Related

Related tools & reading