Skip to content

formatRelativeTime

Playground

call
result 
formatRelativeTime(value: string, locale?: string, options?: FormatRelativeTimeOptions): string
import { formatRelativeTime } from "@northguild/gmt/plain/format";

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.
  • roundingMethod controls how the distance rounds to the display unit.
Parameter Type Description
value string ISO time 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

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

formatRelativeTime("14:30:00", "en-US", { style: "short" }) // "2 hr. ago"
formatRelativeTime(value, "en-US", { roundingMethod: "floor" }) // rounds toward the earlier boundary
formatRelativeTime("not-a-time") // ""

plain/format/formatRelativeTime.ts