Skip to content

relative-time

Imported from yugajs/extension/relative-time.

Property Type Description
rounding? (value) => number Rounds the count of a step, Math.round by default.
thresholds? readonly RelativeTimeThreshold[] The steps, Day.js’s defaults: 44 seconds, 89 seconds, 44 minutes, 89 minutes, 21 hours, 35 hours, 25 days, 45 days, 10 months and 17 months.

A locale’s relativeTime, Day.js’s object: future and past wrap the words, the other keys are the words (s seconds, m a minute, mm minutes, h, hh, d, dd, M, MM, y, yy).

[key: string]:
| string
| ((count, withoutSuffix, key, isFuture) => string)
| ((words) => string)
| undefined
Property Type
d? RelativeTimeFormatter
dd? RelativeTimeFormatter
future RelativeTimeWrapper
h? RelativeTimeFormatter
hh? RelativeTimeFormatter
m? RelativeTimeFormatter
M? RelativeTimeFormatter
mm? RelativeTimeFormatter
MM? RelativeTimeFormatter
past RelativeTimeWrapper
s? RelativeTimeFormatter
y? RelativeTimeFormatter
yy? RelativeTimeFormatter

A step of the thresholds, the first one that fits is used. The same as Day.js’s.

Property Type Description
d? yuga.Unit The unit the count is measured in, the following steps without one reuse it.
l string The key of the words, "s", "m", "mm", "h", "hh", "d", "dd", "M", "MM", "y" or "yy". Singular keys (“a minute”) never show their count.
r? number The largest rounded count the step fits, the last step has none.
type RelativeTimeFormatter = string | ((count, withoutSuffix, key, isFuture) => string);

A word of a locale’s relativeTime, %d is the count. A function gets the count, withoutSuffix, the key ("mm") and whether it’s the future.


type RelativeTimeWrapper = string | ((words) => string);

What future and past wrap the words with, %s is the words ("in %s"). A function gets the finished words ("2 hours"), the same as Day.js.

const relativeTime: yuga.Extension<
| RelativeTimeConfig
| undefined>;

Relative time, Day.js’s relativeTime plugin: fromNow, from, toNow and to.

yuga.extend(relativeTime, { thresholds, rounding });

en is Day.js’s English (“a few seconds ago”), other locales use Intl.RelativeTimeFormat (“il y a 2 heures”) unless they have a relativeTime object.


const relativeWords: (measure, localeName, withoutSuffix?, config?) => string;

A difference in words, what fromNow, from, toNow and to use (and what a duration’s humanize can reuse).

Parameter Type Description
measure (unit) => number The signed difference in a unit ("second", "minute", "hour", "day", "month", "year"), positive is the future. Fractions are rounded by the thresholds. It is only called for the units the steps reach.
localeName string A registered locale (an unknown one is English).
withoutSuffix? boolean Drop the “ago” / “in”.
config? RelativeTimeConfig The thresholds and rounding, the ones relativeTime was installed with when omitted (Day.js’s defaults without them).

string

The words, or the locale’s invalidDate when the difference is NaN. The same as Day.js’s fromToBase, including a step without a unit reusing the previous count, and a count of 1 or less using the previous step (“1 minute” is “a minute”).


const rtfOf: (name, auto?) => Intl.RelativeTimeFormat;

The cached Intl.RelativeTimeFormat of a locale, auto says “yesterday” and “tomorrow” for a day (numeric: "auto"). Throws for a name Intl rejects.

Parameter Type
name string
auto? boolean

Intl.RelativeTimeFormat