tw.date
Side: Shared (Client + Server)
Utilities for timestamps and duration formatting.
Functions
tw.date.now()
Returns the current time in milliseconds.
- Server: uses
os.time() * 1000 - Client: uses
GetGameTimer()
Returns: integer (milliseconds)
tw.date.format_duration(duration_ms, labels?)
Formats a duration in milliseconds into a human-readable string.
| Parameter | Type | Default | Description |
|---|---|---|---|
duration_ms | integer | — | Duration in milliseconds |
labels | table? | See below | Custom labels for units |
Labels table:
| Field | Type | Default | Description |
|---|---|---|---|
ms | string | "ms" | Milliseconds unit |
s | string | "s" | Seconds unit |
min | string | "min" | Minutes unit |
h | string | "h" | Hours unit |
d | string | "d" | Days unit (singular) |
ds | string | "d" | Days unit (plural) |
separator | string | ", " | Separator between units |
now | string | "now" | Returned when duration is 0 or negative |
Returns: string
Examples
lua
-- Current timestamp
local now = tw.date.now()
-- Format durations
tw.date.format_duration(9015000)
-- "2h, 30 min, 15 s"
tw.date.format_duration(500)
-- "500 ms"
tw.date.format_duration(0)
-- "now"
tw.date.format_duration(90061000)
-- "1 d, 1h, 1 min, 1 s"
-- Custom labels (French)
tw.date.format_duration(9015000, {
h = "heure(s)",
min = "minute(s)",
s = "seconde(s)",
separator = " et ",
})
-- "2 heure(s) et 30 minute(s) et 15 seconde(s)"
