Skip to content

Format

Money in integer minor units, relative days and times, and plain-text extraction for truncated cells.

formatCents(cents, currency) writes money held as whole minor units: the currency code, thousands grouped, always two decimals. It never divides as a float, so large amounts stay exact. formatRelativeDays(days) is today for 0 and otherwise the count with day or days. formatRelativeTime(value) turns a timestamp into just now, 5 min ago, 3 h ago, yesterday or 4 days ago; it reads the clock, so a time close to a boundary can differ between the server's HTML and the browser. plainText(node) returns a node's text when it is a string or a number, for the title of a truncated cell.

Money from minor units

formatCents(480000, "SGD")
SGD 4,800.00
formatCents(1999, "EUR")
EUR 19.99
formatCents(5, "USD")
USD 0.05
formatCents(-98000, "SGD")
SGD -980.00
formatCents(123456789012345, "USD")
USD 1,234,567,890,123.45

Relative days

  • Send the release notesDue today
  • Confirm the launch dateDue in 1 day
  • Review the pricing page copyDue in 5 days
  • Migrate the staging databaseDue in 30 days

Relative time

  • Invoice INV-1042 was sentjust now
  • Release 4.2 was published5 min ago
  • The staging database was migrated3 h ago
  • The launch date was confirmedyesterday
  • The pricing page copy was approved4 days ago

Plain text for a title

  • Review the pricing page copy before the launch
  • 1042
  • In review

Installation

npx shadcn@latest add @khaeli/format

Installs with

Nothing else. This item needs no other Registry item and no npm package.

Accessible name

  • A relative time loses the exact moment. Where it matters, keep the full date nearby, for example on the record's own page.
  • A title is a convenience for pointer users. Text that must be read in full needs room, not a tooltip.

Registry JSON

The item as the shadcn CLI reads it, with every file it ships: https://ui.khaeli.com/r/format.json