View Source Timex.Format.DateTime.Formatters.Relative (timex v3.7.11)
Relative time, based on Moment.js
Uses localized strings.
The format string should contain {relative}, which is where the phrase will be injected.
| Range | Sample Output
0 seconds | now | |
1 to 45 seconds | a few seconds ago | |
45 to 90 seconds | a minute ago | |
90 seconds to 45 minutes | 2 minutes ago ... 45 minutes ago | |
45 to 90 minutes | an hour ago | |
90 minutes to 22 hours | 2 hours ago ... 22 hours ago | |
22 to 36 hours | a day ago | |
36 hours to 25 days | 2 days ago ... 25 days ago | |
25 to 45 days | a month ago | |
45 to 345 days | 2 months ago ... 11 months ago | |
345 to 545 days (1.5 years) | a year ago | |
546 days+ | 2 years ago ... 20 years ago |
Link to this section Summary
Functions
Callback implementation for Timex.Format.DateTime.Formatter.format!/2
.
Formats a date/time as a relative time formatted string
Callback implementation for Timex.Format.DateTime.Formatter.lformat!/3
.
Callback implementation for Timex.Format.DateTime.Formatter.lformat/3
.
Callback implementation for Timex.Format.DateTime.Formatter.tokenize/1
.
Link to this section Functions
@spec format!(Timex.Types.calendar_types(), String.t()) :: String.t() | no_return()
Callback implementation for Timex.Format.DateTime.Formatter.format!/2
.
@spec format(Timex.Types.calendar_types(), String.t()) :: {:ok, String.t()} | {:error, term()}
Formats a date/time as a relative time formatted string
examples
Examples
iex> Elixir.Timex.Format.DateTime.Formatters.Relative.format(Timex.shift(Timex.now, minutes: -1), "{relative}")
{:ok, "1 minute ago"}
@spec lformat!(Timex.Types.calendar_types(), String.t(), String.t()) :: String.t() | no_return()
Callback implementation for Timex.Format.DateTime.Formatter.lformat!/3
.
@spec lformat(Timex.Types.calendar_types(), String.t(), String.t()) :: {:ok, String.t()} | {:error, term()}
Callback implementation for Timex.Format.DateTime.Formatter.lformat/3
.
@spec tokenize(String.t()) :: {:ok, [Timex.Parse.DateTime.Tokenizers.Directive.t()]} | {:error, term()}
Callback implementation for Timex.Format.DateTime.Formatter.tokenize/1
.