@northguild/gmt
Version:
Date and time library for JavaScript and TypeScript, built on TC39 Temporal: time zones, DST, business days, ISO 8601 strings in and out. Never throws.
40 lines • 2.27 kB
TypeScript
import { RelativeTimeFormatOptions } from "../../types/relative-time-options.js";
import { RelativeTimeUnit } from "../../types/relative-time-unit.js";
import "../../types/index.js";
/**
* Options for `formatRelativeTime`: the reference time, the display unit, the rounding and the
* wording.
*/
export interface FormatRelativeTimeOptions extends RelativeTimeFormatOptions {
/**
* The unit the distance is written in, whatever its size: `"second"`, `"minute"` or `"hour"`,
* singular or plural. Any other value returns `""`, a date unit such as `"day"` included: a time
* has no date, and Temporal throws RangeError for a unit outside the units of the type. Omitted,
* the unit is picked from the distance: second under a minute, minute under an hour and hour
* beyond.
*
* @defaultValue None. The unit is picked from the distance.
*/
largestUnit?: RelativeTimeUnit | `${RelativeTimeUnit}s`;
}
/**
* Format the relative time between a plain time and a reference time.
*
* - Auto-picks the display unit (second/minute/hour) based on the distance, unless
* `largestUnit` forces one.
* - `largestUnit` is one of those three units, singular or plural. Any other value returns `""`.
* - `options` must be an object or omitted: `null` or any other primitive returns `""`, as
* Temporal's GetOptionsObject rejects it.
*
* @param value ISO time string to format
* @param locale optional: BCP 47 locale tag, or a preference list of tags (ECMA-402)
* @param options How the distance is measured, rounded and worded
* @returns the formatted relative-time string, or "" on invalid input
*
* @example formatRelativeTime("14:30:00", "de-DE", { style: "short", reference: "16:30:00" }) // "vor 2 Std."
* @example formatRelativeTime("09:00:00", "en-GB", { reference: "10:30:00", roundingMethod: "floor" }) // "2 hours ago" (−1.5 hours floors to −2)
* @example formatRelativeTime("not-a-time") // ""
* @example formatRelativeTime("10:00:00", ["fr-FR", "en-US"], { reference: "12:00:00" }) // "il y a 2 heures"
* @example formatRelativeTime("10:00:00", "ja-JP", null as never) // "" (null options)
*/
export declare function formatRelativeTime(value: string, locale?: string | string[], options?: FormatRelativeTimeOptions): string;