Skip to content

formatRelativeUtc

formatRelativeUtc(value: string, locale?: string, options?: FormatRelativeUtcOptions): string
import { formatRelativeUtc } from "@northguild/gmt/utc/format";

Format the relative time between a UTC ISO string and a reference instant.

  • Auto-picks the display unit (second through year) based on the distance, unless largestUnit forces one.
  • roundingMethod controls how the distance rounds to the display unit.
Parameter Type Description
value string UTC ISO string to format
locale string optional: BCP 47 locale tag

options

Option Type Default
style? "long" | "short" | "narrow"
numeric? "always" | "auto"
largestUnit? RelativeUnit
roundingMethod? RelativeRoundingMethod
reference? string
timeZone? string

the formatted relative-time string, or “” on invalid input

formatRelativeUtc("2024-03-17T14:30:45+00:00[UTC]", "en-US") // "2 years ago"
formatRelativeUtc(value, "en-US", { roundingMethod: "floor" }) // rounds toward the earlier boundary
formatRelativeUtc("not-a-date") // ""

utc/format/formatRelativeUtc.ts