The utility library contains a set of useful helper functions for dates and times.
This section is server side only.
Gets a current os.time stamp and returns both timestamp and a formatted version.
Copy 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
Copy --- Retrieve time data.
local data = utils.dates. get_timestamp ()
--- Print time data.
print ( 'Timestamp:' , data.ts)
print ( 'Formatted Timestamp:' , data.formatted
Converts a UNIX timestamp to a human readable date and time.
Copy 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
Copy --- Convert timestamp.
local data = utils.dates. convert_timestamp ( 1628759592 )
--- Print data
print ( 'Date:' , data.date)
print ( 'Time:' , data.time)
print ( 'Formatted:' , data.formatted)