Skip to content
Focsd homeFocsd homeFocsd
Forms

Relative time

Shows when something happened, or will happen, as "3 minutes ago" or "in 2 hours", and keeps the text current.

  • Tag: focsd-relative-time
  • Pattern: Time element (Intl.RelativeTimeFormat)
  • Category: Utility

Example

Comment posted

Invoice sent

Next backup

Zuletzt gespeichert

HTML
<p>
Comment posted
<focsd-relative-time datetime="2026-10-15T10:41:00Z"></focsd-relative-time>
</p>
<p>
Invoice sent
<focsd-relative-time
datetime="2026-10-05T09:00:00Z"
format="auto"></focsd-relative-time>
</p>
<p>
Next backup <focsd-relative-time datetime="2026-10-16T02:00:00Z"></focsd-relative-time>
</p>
<p lang="de">
Zuletzt gespeichert
<focsd-relative-time datetime="2026-10-15T09:30:00Z"></focsd-relative-time>
</p>

Usage

Import the module once, then use the tag like any HTML element. Every framework below renders the same element; see Frameworks for setup.

<script type="module">
import '@focsd-dev/components/focsd-relative-time.js';
</script>
<focsd-relative-time datetime="2026-10-15T10:41:00Z"></focsd-relative-time>

API

Attributes

AttributePropertyTypeDefault
datetimedatetimestring—
formatformat'relative' | 'auto'relative
frozenfrozenbooleanfalse
localelocalestring—

CSS parts

Style the inside of the component with ::part(), for example focsd-relative-time::part(time).

PartDescription
timeThe `<time>` element.

Accessibility

  • The text sits in a <time> element whose datetime is the exact moment, so scripts and machines can read it.
  • The full date and time follows the relative text as visually hidden text, so a screen reader reads "3 minutes ago, October 15, 2026 at 10:41 AM". The same text is the tooltip.
  • It has no live region: the text updates once a minute (once an hour for older times) without being announced, which would be noise.
  • With format="auto", times more than 7 days away show the date instead, which is easier to place than "3 weeks ago".
  • Without locale it follows the nearest lang attribute. With locale the text carries that lang, so screen readers read it in that language.
  • Server-rendered pages show the text for the moment the server rendered it; it corrects itself when the element loads in the browser.