UNPKG

@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.34 kB
import { RelativeDateUnit } from "../../types/relative-date-unit.js"; import { RelativeTimeFormatOptions } from "../../types/relative-time-options.js"; import "../../types/index.js"; /** * Options for `formatRelativeDate`: the reference date, the display unit, the rounding and the * wording. */ export interface FormatRelativeDateOptions extends RelativeTimeFormatOptions { /** * The unit the distance is written in, whatever its size: `"day"`, `"week"`, `"month"` or * `"year"`, singular or plural. Any other value returns `""`, a time unit such as `"hour"` * included: a date has no time, and Temporal throws RangeError for a unit outside the units of * the type. Omitted, the unit is picked from the distance: day under 7 days, week under 28, * month under 365 and year beyond. * * @defaultValue None. The unit is picked from the distance. */ largestUnit?: RelativeDateUnit | `${RelativeDateUnit}s`; } /** * Format the relative time between a plain date and a reference date. * * - Auto-picks the display unit (day/week/month/year) based on the distance, unless * `largestUnit` forces one. * - `largestUnit` is one of those four 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 date 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 formatRelativeDate("2026-01-15", "pt-BR", { reference: "2026-04-15" }) // "há 3 meses" * @example formatRelativeDate("2026-03-01", "en-GB", { reference: "2026-03-11", largestUnit: "week", roundingMethod: "floor" }) // "2 weeks ago" (−1.43 weeks floors to −2; the default rounds to "last week") * @example formatRelativeDate("not-a-date") // "" * @example formatRelativeDate("2024-01-15", ["fr-FR", "en-US"], { reference: "2024-03-15" }) // "il y a 2 mois" * @example formatRelativeDate("2024-03-12", "ar-EG", null as never) // "" (null options) */ export declare function formatRelativeDate(value: string, locale?: string | string[], options?: FormatRelativeDateOptions): string;