@molecule/app-relative-time-svelte

Feature · relative-time · App (browser) · v1.1.2 · Apache-2.0

formatRelativeTime utility + RelativeTime component with auto-refresh

npm install @molecule/app-relative-time-svelte

npm · Source on GitHub

How it works

@molecule/app-relative-time-svelte is a ready-made relative-time feature for the app (browser) side. It composes the core interfaces it needs, so it works with whichever providers your app has bonded.

<script lang="ts">
  import { RelativeTime, formatRelativeTime } from '@molecule/app-relative-time-svelte'

  const label = formatRelativeTime('2024-06-01T10:00:00Z', Date.now(), 'en-US')
</script>

<!-- Component — auto-refreshes every minute -->
<RelativeTime date="2024-06-01T10:00:00Z" locale="en-US" titleLocale="en-US" />

Works with: @molecule/app-svelte, @molecule/app-ui, @molecule/app-ui-svelte

Reference

Svelte relative-time formatting.

Exports:

  • formatRelativeTime(date, now?, locale?) — utility using Intl.RelativeTimeFormat.
  • RelativeTime — live-updating component ("5 minutes ago").

It is the Svelte counterpart of @molecule/app-relative-time-react: the same export names, the same props (React names) and the same DOM for the same input.

Auto-generated, AI-first package reference for the molecule.dev ecosystem. It is written to be read by coding agents as much as by people, and is generated from this package's source — edit src/index.ts JSDoc, not this file.

Quick Start

<script lang="ts">
  import { RelativeTime, formatRelativeTime } from '@molecule/app-relative-time-svelte'

  const label = formatRelativeTime('2024-06-01T10:00:00Z', Date.now(), 'en-US')
</script>

<!-- Component — auto-refreshes every minute -->
<RelativeTime date="2024-06-01T10:00:00Z" locale="en-US" titleLocale="en-US" />

Type

feature

Installation

npm install @molecule/app-relative-time-svelte @molecule/app-svelte @molecule/app-ui @molecule/app-ui-svelte svelte

API

Interfaces

RelativeTimeProps

Props accepted by the RelativeTime component.

interface RelativeTimeProps {
  /** ISO string, Date, or epoch ms. */
  date: Date | string | number
  /**
   * Locale override. Defaults to the app's active locale (`useLocale()`), so the
   * label follows a locale switch; an explicit `locale` still wins.
   */
  locale?: string
  /** Auto-refresh interval (ms). Defaults to 60_000 (1 min). Set 0 to disable. */
  refreshMs?: number
  /** When provided, renders the absolute date in a `title` attribute for hover tooltip. */
  titleLocale?: string
  /** Extra classes (applies to the surrounding `<time>` element). */
  className?: string
}

Functions

formatRelativeTime(input, now, locale)

Formats a date, timestamp, or epoch number as a human-readable relative time string (e.g. "5 minutes ago", "in 3 days") using Intl.RelativeTimeFormat.

function formatRelativeTime(
  input: Date | string | number,
  now?: Date | number,
  locale?: string,
): string

Constants

RelativeTime

const RelativeTime: LegacyComponentType

Injection Notes

Requirements

Peer dependencies:

  • @molecule/app-svelte ^3.5.0 || ^4.0.0
  • @molecule/app-ui ^1.0.1
  • @molecule/app-ui-svelte ^1.1.0
  • svelte ^5.0.0

Runtime Dependencies

  • @molecule/app-svelte

  • @molecule/app-ui

  • @molecule/app-ui-svelte

  • svelte

  • Zero molecule wiring required — no ClassMap, no bonds; safe in any Svelte tree. RelativeTime reads the app's active i18n locale through useLocale() (a default English provider serves when none is bonded).

  • RelativeTime props: date (Date | ISO string | epoch ms), locale?, refreshMs? (default 60000 — pass 0 to disable the per-instance timer, cheaper in long lists), titleLocale? (adds an absolute-date title tooltip, formatted in that locale), className?.

  • locale defaults to the app's active i18n locale (useLocale()), so the label follows a locale switch; an explicit locale prop still wins.

  • When Intl.RelativeTimeFormat is unavailable, fallback strings ("just now", "5m ago") are English-only.