Skip to content
Nana UI

Formatters

Relative Time

Renders a localised, auto-updating relative timestamp — "2 hours ago", "in 3 days" — from a date. It refreshes itself on a cadence proportional to the elapsed time (every second under a minute, every minute under an hour, and so on) and outputs a semantic <time> element carrying the machine readable datetime plus an absolute title for accessibility.

View .md Download
  • Paid
  • Reading taken
  • Lease ends

Install the package for a versioned dependency:

npm install @nana-tec/ui-components

Or copy the source in and own it:

npx @nana-tec/ui add relative-time --framework web-components
npx @nana-tec/ui add relative-time --framework svelte

Lands in src/lib/components/ui/relative-time/ as NanaRelativeTime.svelte, styled with Tailwind classes that resolve to the design tokens. Requires Tailwind v4.

Import the autoloader once, in your app's entry point. Every <nana-*> tag then works anywhere, and each component downloads only the first time a page uses it:

import "@nana-tec/ui-components/autoloader";
import "@nana-tec/ui-components/theme";

The second line gives every component the Nana look and the design tokens behind it. Leave it out to style the components entirely yourself.

Import a component directly when it must be ready at once: on screen at first paint, or when you call its methods straight away. The two mix freely:

import "@nana-tec/ui-components/relative-time";

Copied in with npx @nana-tec/ui add relative-time? Import your project's own @/lib/nana-ui/autoloader.js once instead; the CLI keeps it up to date.

<script lang="ts">
import { NanaRelativeTime } from '$lib/components/ui/relative-time';
</script>
<NanaRelativeTime date="2026-03-10T09:30:00Z" />

Renders a <time> with the exact moment in datetime and the full date in its title. It refreshes as time passes, more often for recent moments; set live={false} to fix it.

<nana-relative-time date="2026-01-01T00:00:00Z"></nana-relative-time>

Properties

PropertyAttributeTypeDefaultDescription
datedateDate | string | numbernew Date()The date — a Date, an ISO string, or a timestamp
formatformatRelativeTimeStyle'long'Wording length passed to Intl.RelativeTimeFormat
livelivebooleantrueKeep the value refreshing as time passes
localelocalestring | undefined—BCP 47 locale (defaults to the document locale)

CSS parts

PartDescription
::part(time)The time element

NanaRelativeTime

PropTypeDefault
daterequiredDate | string | number
formatRelativeTimeStyle'long'
localestring
livebooleantrue

Also accepts HTMLAttributes<HTMLTimeElement>.