Skip to content

RelativeTime ​

Renders epoch milliseconds / ISO 8601 / Date as a localized relative time, backed by a semantic <time datetime> element. Optional auto-refresh keeps it fresh and pauses while the page is hidden. Invalid or missing values render a safe empty state — never NaN / Invalid Date.

Basic Usage ​

value accepts epoch milliseconds (numeric string) or ISO 8601; the property channel additionally accepts number / Date (a Date is written back as an ISO attribute).

A purely numeric value is always parsed as epoch milliseconds: value="2026" means 1970-01-01T00:00:02.026Z, not the year 2026 — write a year as ISO 8601 such as 2026-01-01.

For ISO 8601 include Z or an explicit offset (e.g. 2026-10-29T12:00:00+08:00): an offset-less 2026-10-29T12:00:00 is parsed in the runtime's local time zone, while date-only 2026-10-29 is parsed as UTC; the inner <time datetime> output is always UTC ISO 8601.

epoch / ISO / Date property

Units and Direction ​

Seconds / minutes / hours / days / months / years use fixed factors and round to the nearest value; reaching the next unit's ceiling promotes the unit (59.5s → 1 minute, 23.5h → 1 day, 12 months → 1 year). Future values are positive, past values negative.

Past and future (values injected from the current clock)

Semantic <time> ​

For valid values the inner <time part="time"> carries a datetime attribute synchronized with the resolved instant (UTC ISO 8601), ready for crawlers, screen readers or host scripts. aria-live stays off so auto-refresh does not interrupt assistive technology.

Read the datetime attribute

Locale and numeric ​

locale takes precedence over the nearest oas-config-provider, then falls back to the global i18n locale and finally en-US — the default @oas-ui/i18n locale is zh-CN, so en-US only acts as a functional fallback when the whole candidate chain fails; malformed tags (such as zh_CN) never throw. The property mirrors the attribute: el.locale = 'en-US' writes the locale attribute back, and null removes it. numeric="auto" (default) lets Intl.RelativeTimeFormat produce idiomatic "now / yesterday", while numeric="always" always shows a number.

en-US / zh-CN / invalid locale fallback / auto vs always

Auto Refresh ​

auto-refresh is enabled by presence ("false" disables it): second-level units tick every second, all other units throttle at 30 seconds. Hiding the page pauses the timer; becoming visible recomputes immediately. Disconnecting clears the timer and listener; reconnecting restores them.

Ticks from 2 seconds ago / disabled auto-refresh

Empty State ​

A missing or invalid value renders no text and no datetime; raw keys / NaN / Invalid Date never appear.

Invalid and missing values

Invalid: | Missing:

Styling ​

The host uses --oas-color-text-secondary by default and inherits font size; ::part(time) overrides color and weight.

::part(time) customization

API ​

oas-relative-time ​

Attributes ​

AttributeDescriptionTypeDefault
auto-refreshThrottled auto refresh (1s for second-level units, 30s otherwise); "false" or absent disables it; pauses while hidden, cleared on disconnect, restored on reconnectstring—
localeRelative-time locale (e.g. zh-CN / en-US); invalid or unsupported tags safely fall back to provider → global i18n → en-US (global default is zh-CN, en-US is only a fallback)string—
numericIntl numeric mode: auto (default, idiomatic “now / yesterday”) / always (always numeric); other values use autostringauto
valueTarget instant: epoch milliseconds or ISO 8601 (purely numeric values are always epoch milliseconds — write a year as 2026-01-01); offset-less ISO is parsed in the runtime's local time zone and date-only as UTC, while datetime output is always UTC; the property channel also accepts number / Datestring—

Property (JS property; see each row for attribute reflection) ​

PropertyDescriptionTypeDefault
localeRelative-time locale property channel: writes the locale attribute back, null removes it (falls back to provider → global i18n → en-US); reading returns the raw attribute (string)string | null—
valueTarget instant property channel: number is epoch milliseconds, Date is written back as an ISO attribute, null clears (empty state); reading returns the raw value attribute (string)string | number | Date | null—

Released under the MIT OR Apache-2.0 License.