Date & Time

The utility library contains a set of useful helper functions for dates and times. This section is server side only.

Functions

get_timestamp()

Gets a current os.time stamp and returns both timestamp and a formatted version.

Function

local function get_timestamp()
    local ts = os.time()
    return { timestamp = ts, formatted = os.date('%Y-%m-%d %H:%M:%S', ts) }
end

exports('dates_get_timestamp', get_timestamp)
utils.dates.get_timestamp = get_timestamp

Example

--- Retrieve time data.
local data = utils.dates.get_timestamp()

--- Print time data.
print('Timestamp:', data.ts)
print('Formatted Timestamp:', data.formatted

convert_timestamp()

Converts a UNIX timestamp to a human readable date and time.

Function

local function convert_timestamp(timestamp)
    return { date = os.date('%Y-%m-%d', timestamp), time = os.date('%H:%M:%S', timestamp), formatted = os.date('%Y-%m-%d %H:%M:%S', timestamp) }
end

exports('dates_convert_timestamp', convert_timestamp)
utils.dates.convert_timestamp = convert_timestamp

Example

--- Convert timestamp.
local data = utils.dates.convert_timestamp(1628759592)

--- Print data
print('Date:', data.date)
print('Time:', data.time)
print('Formatted:', data.formatted)

Last updated