Skip to content

HyRelativeTime

Usage examples on this page are written for Lit / plain HTML (<hy-relative-time>). The same component ships as HyRelativeTime in @whitespaceux/harmony-react (native React 19), with typed wrappers for Angular, Solid, Svelte, and Vue. The API reference below applies to all frameworks.

Renders a date as a relative phrase ("3 minutes ago", "in 2 days"), auto-updating as time passes.

Wraps a native <time datetime> so the precise timestamp is always in the DOM for screen readers and tooling. Phrasing comes from Intl.RelativeTimeFormat; the elapsed-duration math runs on Temporal (via the shared ensureTemporal() gate — native on evergreen engines, polyfilled only on Safari / non-Temporal runtimes) so unit boundaries are calendar-accurate rather than fixed-second approximations.

SSR safety: relative time is non-deterministic, so the server and the first client render emit the absolute date (deterministic), then upgrade to relative on mount — avoiding a hydration mismatch.

Key Features:

  • format: long / short / narrow
  • sync: self-managing refresh that tightens near, relaxes far, and pauses on document.hidden
  • threshold: past a cutoff, fall back to the absolute formatted date
  • Past and future tense; locale-aware

Examples

html
<hy-relative-time date="2026-06-12T10:00:00Z" sync></hy-relative-time>

API

Properties

PropertyAttributeTypeDefaultDescription
datedatestring | number | undefinedThe target date — ISO string or epoch ms.
formatformat'long' | 'short' | 'narrow''long'Phrasing granularity.
syncsyncbooleanfalseAuto-refresh as time passes.
thresholdthresholdnumber | undefinedPast this elapsed ms, render the absolute formatted date instead of relative.
localelocalestring | undefinedBCP-47 locale (defaults to the environment locale).
nowstring | number | Date | undefinedInjectable "current time" for deterministic tests / stories. Property-only.

Slots

SlotDescription
defaultNo slots; the element renders its own <time>.

CSS Parts

PartDescription
timeThe inner <time> element.

Built with Lit. Documented with VitePress.