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
import { formatCents } from "@/lib/format"
// Amounts are whole cents, as they are stored.
const amounts: [cents: number, currency: string][] = [
[480000, "SGD"],
[1999, "EUR"],
[5, "USD"],
[-98000, "SGD"],
[123456789012345, "USD"],
]
export default function FormatMoneyFromMinorUnits() {
return (
<dl className="flex w-full max-w-md flex-col divide-y divide-border border-y border-border">
{amounts.map(([cents, currency]) => (
<div key={`${cents} ${currency}`} className="flex flex-col gap-1 py-2">
<dt className="font-mono text-sm text-muted-foreground">
formatCents({cents}, "{currency}")
</dt>
<dd className="tabular-nums">{formatCents(cents, currency)}</dd>
</div>
))}
</dl>
)
}
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
import { formatRelativeDays } from "@/lib/format"
const tasks = [
{ title: "Send the release notes", dueIn: 0 },
{ title: "Confirm the launch date", dueIn: 1 },
{ title: "Review the pricing page copy", dueIn: 5 },
{ title: "Migrate the staging database", dueIn: 30 },
]
export default function FormatRelativeDays() {
return (
<ul className="flex w-full max-w-sm flex-col divide-y divide-border border-y border-border">
{tasks.map(({ title, dueIn }) => (
<li key={title} className="flex items-center justify-between gap-4 py-2">
<span className="min-w-0 truncate">{title}</span>
<span className="shrink-0 text-muted-foreground tabular-nums">
{dueIn === 0 ? "Due today" : `Due in ${formatRelativeDays(dueIn)}`}
</span>
</li>
))}
</ul>
)
}
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
"use client"
import { useState } from "react"
import { formatRelativeTime } from "@/lib/format"
const SECOND = 1000
const MINUTE = 60 * SECOND
const HOUR = 60 * MINUTE
const DAY = 24 * HOUR
// How long ago each thing happened. A product has the timestamp itself, from its data.
const activity = [
{ text: "Invoice INV-1042 was sent", ago: 20 * SECOND },
{ text: "Release 4.2 was published", ago: 5 * MINUTE },
{ text: "The staging database was migrated", ago: 3 * HOUR },
{ text: "The launch date was confirmed", ago: DAY },
{ text: "The pricing page copy was approved", ago: 4 * DAY },
]
export default function FormatRelativeTime() {
// Timestamps counted back from the moment the example renders, as ISO strings.
const [events] = useState(() =>
activity.map(({ text, ago }) => ({ text, at: new Date(Date.now() - ago).toISOString() })),
)
return (
<ul className="flex w-full max-w-sm flex-col divide-y divide-border border-y border-border">
{events.map(({ text, at }) => (
<li key={text} className="flex items-center justify-between gap-4 py-2">
<span className="min-w-0 truncate">{text}</span>
<span className="shrink-0 text-muted-foreground tabular-nums">{formatRelativeTime(at)}</span>
</li>
))}
</ul>
)
}
Plain text for a title
- Review the pricing page copy before the launch
- 1042
- In review
import type { ReactNode } from "react"
import { Badge } from "@/components/ui/badge"
import { plainText } from "@/lib/format"
/** A cell that cuts long text short and keeps the whole of it in `title` when it is plain text. */
function Cell({ children }: { children: ReactNode }) {
return (
<span className="block truncate" title={plainText(children)}>
{children}
</span>
)
}
export default function FormatPlainTextForATitle() {
return (
<ul className="flex w-full max-w-48 flex-col divide-y divide-border border-y border-border">
<li className="py-2">
<Cell>Review the pricing page copy before the launch</Cell>
</li>
<li className="py-2">
<Cell>{1042}</Cell>
</li>
<li className="py-2">
{/* An element is not plain text: plainText returns undefined and no title is set. */}
<Cell>
<Badge variant="secondary">In review</Badge>
</Cell>
</li>
</ul>
)
}
Installation
npx shadcn@latest add @khaeli/formatbunx --bun shadcn@latest add @khaeli/formatInstalls 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
titleis 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