# `Timex.Format.DateTime.Formatters.Relative`
[🔗](https://github.com/bitwalker/timex/blob/4.0.0/lib/format/datetime/formatters/relative.ex#L1)

Localized relative date and time formatting.

The format string must contain `{relative}`. Date targets compare calendar
days; datetime targets compare elapsed time after conversion to UTC.

Elapsed values truncate to whole seconds toward zero, then to whole units
within the following ranges. The same thresholds apply in both directions.
A month is an approximate 30 days and a year is 360 days in this formatter.

| Absolute elapsed time | Past output | Future output |
| --- | --- | --- |
| 0 whole seconds | now | now |
| 1–45 seconds | N seconds ago | in N seconds |
| 46 seconds to <2 minutes | 1 minute ago | in 1 minute |
| 2 minutes to <1 hour | N minutes ago | in N minutes |
| 1 hour to <2 hours | 1 hour ago | in 1 hour |
| 2 hours to <24 hours | N hours ago | in N hours |
| 24 hours to <48 hours | yesterday | tomorrow |
| 48 hours to <30 days | N days ago | in N days |
| 30 days to <60 days | 1 month ago | in 1 month |
| 60 days to <360 days | N months ago | in N months |
| 360 days to <720 days | 1 year ago | in 1 year |
| 720 days or more | N years ago | in N years |

`Timex.from_now/1` samples the clock when formatting. A target computed as
two days after an earlier clock sample can therefore be less than 48 hours
away and render as "tomorrow". Pass the same reference to `Timex.from_now/2`
or `relative_to/3` for deterministic boundaries. Use Date targets when the
intended comparison is calendar days rather than elapsed hours.

# `relative_date`

```elixir
@type relative_date() ::
  Date.t()
  | NaiveDateTime.t()
  | DateTime.t()
  | Timex.Types.date()
  | Timex.Types.datetime()
  | Timex.Types.microsecond_datetime()
```

# `format`

```elixir
@spec format(relative_date(), String.t()) :: {:ok, String.t()} | {:error, term()}
```

Formats a date/time as a relative time formatted string.

Date targets (including Erlang date tuples) compare calendar days with today's
UTC date. Datetime targets compare elapsed time. Time-only targets are unsupported.

## Examples

    iex> Elixir.Timex.Format.DateTime.Formatters.Relative.format(Timex.shift(Timex.now, minutes: -1), "{relative}")
    {:ok, "1 minute ago"}

# `format!`

```elixir
@spec format!(relative_date(), String.t()) :: String.t() | no_return()
```

# `lformat`

```elixir
@spec lformat(relative_date(), String.t(), String.t()) ::
  {:ok, String.t()} | {:error, term()}
```

# `lformat!`

```elixir
@spec lformat!(relative_date(), String.t(), String.t()) :: String.t() | no_return()
```

# `relative_to`

Formats a target relative to an explicit reference.

A Date target compares calendar dates, ignoring the reference's time of day.
An aware reference is first converted to UTC, so its UTC date is used. Datetime
targets compare elapsed time, treating naive values as UTC and date references
as midnight. Use Date targets for calendar-day labels such as "tomorrow".

# `relative_to`

# `tokenize`

```elixir
@spec tokenize(String.t()) ::
  {:ok, [Timex.Parse.DateTime.Tokenizers.Directive.t()]} | {:error, term()}
```

---

*Consult [api-reference.md](api-reference.md) for complete listing*
