@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.
54 lines (52 loc) • 3 kB
JavaScript
import { Temporal } from "../../internal/temporal.js";
import { nowPlainDateTimeISO } from "../../internal/temporalCompat/clock.js";
import { isObject } from "../../internal/isObject.js";
import { durationTotal } from "../../internal/zonedWallClockDifference.js";
import { isValidDateTime } from "../validate/isValidDateTime.js";
import { formatRelativeDuration } from "../../internal/formatRelativeDuration.js";
// src/plain/format/formatRelativeDateTime.ts
/**
* Format the relative time between a plain date-time and a reference date-time.
*
* - Auto-picks the display unit based on the distance, unless `largestUnit` forces one: second
* under a minute, minute under an hour, hour under a day, then `formatRelativeDate`'s thresholds —
* day under 7 days, week under 28, month under 365, year beyond — so a 3-year distance reads
* "3 years ago".
* - **Compatibility:** before 1.16.0 week, month and year were never auto-picked ("1,096 days ago").
* Pass `largestUnit: "day"` to keep a day count.
* - `largestUnit` is one of the seven units from `"second"` to `"year"`, 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-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 formatRelativeDateTime("2026-03-17T09:00:00", "en-GB", { style: "long", reference: "2026-03-17T06:00:00" }) // "in 3 hours"
* @example formatRelativeDateTime("2026-01-15T00:00:00", "en-GB", { reference: "2026-01-15T10:30:00", roundingMethod: "floor" }) // "11 hours ago" (−10.5 hours floors to −11; the default rounds to 10)
* @example formatRelativeDateTime("2021-03-15T12:00:00", "de-DE", { reference: "2024-03-15T12:00:00" }) // "vor 3 Jahren"
* @example formatRelativeDateTime("not-a-date") // ""
* @example formatRelativeDateTime("2024-03-15T09:00:00", ["fr-FR", "en-US"], { reference: "2024-03-15T12:00:00" }) // "il y a 3 heures"
* @example formatRelativeDateTime("2024-03-12T10:00:00", "hi-IN", null as never) // "" (null options)
*/
function formatRelativeDateTime(value, locale, options = {}) {
try {
if (!isObject(options)) return "";
if (!isValidDateTime(value)) return "";
const referenceOption = options.reference;
if (referenceOption !== void 0 && !isValidDateTime(referenceOption)) return "";
try {
const target = Temporal.PlainDateTime.from(value);
const reference = referenceOption ? Temporal.PlainDateTime.from(referenceOption) : nowPlainDateTimeISO();
const diff = target.since(reference);
return formatRelativeDuration(diff, locale, options, (unit) => durationTotal(diff, unit, reference.toPlainDate()));
} catch {
return "";
}
} catch {
return "";
}
}
export { formatRelativeDateTime };