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.

83 lines (81 loc) • 3.02 kB
import { plainDateOf } from "../../internal/inputChecks.js"; import { nowPlainDateISO } from "../../internal/temporalCompat/clock.js"; import { isOptionsObject } from "../../internal/isObject.js"; import { durationTotal } from "../../internal/zonedWallClockDifference.js"; import { resolveRelativeUnit } from "../../internal/resolveRelativeUnit.js"; import { formatRelativeAmount } from "../../internal/formatRelativeDuration.js"; // src/plain/format/formatRelativeDate.ts const DATE_UNITS = [ "year", "month", "week", "day" ]; const AUTO_UNITS = [ { unit: "day", maxDays: 7 }, { unit: "week", maxDays: 28 }, { unit: "month", maxDays: 365 }, { unit: "year", maxDays: Infinity } ]; /** * 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) */ function formatRelativeDate(value, locale, options = {}) { try { if (!isOptionsObject(options)) return ""; const target = plainDateOf(value); if (target === null) return ""; const referenceOption = options.reference; const referenceOptionParsed = plainDateOf(referenceOption); if (referenceOption !== void 0 && referenceOptionParsed === null) return ""; try { const forcedUnit = resolveRelativeUnit(options.largestUnit, DATE_UNITS); const roundingMethod = options.roundingMethod; const reference = referenceOptionParsed ?? nowPlainDateISO(); const diff = target.since(reference); const absDays = Math.abs(diff.total("day")); const unit = forcedUnit === void 0 ? AUTO_UNITS.find((t) => absDays < t.maxDays)?.unit ?? "year" : forcedUnit; let total; try { total = diff.total(unit); } catch { total = durationTotal(diff, unit, reference); } return formatRelativeAmount(total, unit, locale, options, roundingMethod); } catch { return ""; } } catch { return ""; } } export { formatRelativeDate };