Skip to content

OS Time

Standard Lua os time functions. Provides real wall-clock time for timestamps, date formatting, and time calculations.

Global os table. No require needed.

os.time()
os.date()
os.clock()
os.difftime()

Get Unix timestamp (seconds since Jan 1, 1970 UTC):

-- Current timestamp
local now = os.time() -- 1718462445
-- Specific date/time
local t = os.time({
year = 2024,
month = 12,
day = 25,
hour = 10,
min = 30,
sec = 0
})

Signature: os.time([spec]) -> number

Parameters:

FieldTypeDefaultDescription
yearnumbercurrent yearFour-digit year (e.g., 2024)
monthnumbercurrent monthMonth 1-12
daynumbercurrent dayDay of month 1-31
hournumber0Hour 0-23
minnumber0Minute 0-59
secnumber0Second 0-59

When called with no arguments, returns current Unix timestamp.

When called with a table, any missing field uses defaults shown above. The year, month, and day fields default to current date if not specified.

-- Just date (time defaults to midnight)
os.time({year = 2024, month = 6, day = 15})
-- Partial (fills in current year/month)
os.time({day = 1}) -- first of current month

Format a timestamp as string or return a date table:

local now = os.time()

— Default format os.date() — “Sat Jun 15 14:30:45 2024”

— Custom format os.date(“%Y-%m-%d”, now) — “2024-06-15” os.date(“%H:%M:%S”, now) — “14:30:45” os.date(“%Y-%m-%dT%H:%M:%S”, now) — “2024-06-15T14:30:45”

— UTC time (prefix format with !) os.date(”!%Y-%m-%d %H:%M:%S”, now) — UTC instead of local

— Date table local t = os.date(“*t”, now)

Signature: os.date([format], [timestamp]) -> string | table

ParameterTypeDefaultDescription
formatstring"%c"Format string, "*t" for table
timestampnumbercurrent timeUnix timestamp to format
CodeOutputExample
%Y4-digit year2024
%y2-digit year24
%mMonth (01-12)06
%dDay (01-31)15
%HHour 24h (00-23)14
%IHour 12h (01-12)02
%MMinute (00-59)30
%SSecond (00-59)45
%pAM/PMPM
%AWeekday nameSaturday
%aWeekday shortSat
%BMonth nameJune
%bMonth shortJun
%wWeekday (0-6, Sunday=0)6
%jDay of year (001-366)167
%UISO 8601 week number (01-53, week starts Monday)24
%zTimezone offset-0700
%ZTimezone namePDT
%cFull date/timeSat Jun 15 14:30:45 2024
%xDate only06/15/24
%XTime only14:30:45
%%Literal %%

When format is "*t", returns a table:

local t = os.date("*t")
FieldTypeDescriptionExample
yearnumberFour-digit year2024
monthnumberMonth (1-12)6
daynumberDay of month (1-31)15
hournumberHour (0-23)14
minnumberMinute (0-59)30
secnumberSecond (0-59)45
wdaynumberWeekday (1-7, Sunday=1)7
ydaynumberDay of year (1-366)167
isdstbooleanDaylight saving timefalse

Use "!*t" for UTC date table.

Get seconds elapsed since Lua runtime started:

local start = os.clock()
-- do work
for i = 1, 1000000 do end
local elapsed = os.clock() - start
print(string.format("Took %.3f seconds", elapsed))

Signature: os.clock() -> number

Get difference between two timestamps in seconds:

local t1 = os.time({year = 2024, month = 1, day = 1})
local t2 = os.time({year = 2024, month = 12, day = 31})
local diff = os.difftime(t2, t1) -- t2 - t1
local days = diff / 86400
print(days) -- 365

Signature: os.difftime(t2, t1) -> number

ParameterTypeDescription
t2numberLater timestamp
t1numberEarlier timestamp

Returns t2 - t1 in seconds. Can be negative if t1 > t2.

Constant identifying the runtime:

os.platform -- "wippy"