Formats a datetime relative to now, e.g. "3 hours ago" or "in 2 days".
A dependency-free replacement for Timex's {relative} formatting, covering
the ranges a data table typically displays.
Summary
Functions
Returns a human-readable relative time string.
Functions
@spec format(DateTime.t() | NaiveDateTime.t(), DateTime.t()) :: String.t()
Returns a human-readable relative time string.
Accepts a DateTime or a NaiveDateTime (assumed UTC). An optional second
argument fixes the reference "now", which is useful in tests.
Examples
iex> now = ~U[2026-01-01 12:00:00Z]
iex> Slab.Helpers.RelativeTime.format(~U[2026-01-01 09:00:00Z], now)
"3 hours ago"
iex> now = ~U[2026-01-01 12:00:00Z]
iex> Slab.Helpers.RelativeTime.format(~U[2026-01-03 12:00:00Z], now)
"in 2 days"
iex> now = ~U[2026-01-01 12:00:00Z]
iex> Slab.Helpers.RelativeTime.format(~U[2026-01-01 11:59:58Z], now)
"just now"