DocsCore
Formatting
Locale-aware number, currency, date, relative time and list formatting helpers in @generic-translation/core.
On this page
The formatting helpers are small wrappers over Intl with two conveniences: formatter instances are cached, and every helper takes the locale in the same locales option alongside the standard Intl options.
import {
formatNum,
formatCurrency,
formatDateTime,
formatRelativeTime,
formatRelativeTimeFromDate,
formatList,
} from "@generic-translation/core";
locales is a string or an array of strings (a fallback chain), exactly as Intl constructors accept. When omitted, the runtime’s default locale is used, which on a server is rarely what you want: pass it explicitly.
formatNum(value, opts?)
opts is { locales? } & Intl.NumberFormatOptions.
formatNum(1234.5, { locales: "de" }); // "1.234,5"
formatNum(0.256, { locales: "en", style: "percent" }); // "26%"
formatNum(1_250_000, { locales: "en", notation: "compact" }); // "1.3M"
formatNum(12, { locales: "en", style: "unit", unit: "kilometer" });// "12 km"
formatCurrency(value, currency, opts?)
currency is an ISO 4217 code. Other number options still apply.
formatCurrency(9.99, "USD", { locales: "en-US" }); // "$9.99"
formatCurrency(9.99, "EUR", { locales: "de" }); // "9,99 €"
formatCurrency(1200, "JPY", { locales: "ja" }); // "¥1,200"
formatDateTime(value, opts?)
value is a Date, epoch milliseconds, or anything new Date() can parse. opts is { locales? } & Intl.DateTimeFormatOptions. A value that cannot be parsed is returned as a string unchanged rather than throwing.
const d = new Date("2026-11-30T14:05:00Z");
formatDateTime(d, { locales: "en-GB", dateStyle: "long", timeZone: "UTC" });
// "30 November 2026"
formatDateTime(d, { locales: "en-US", dateStyle: "medium", timeStyle: "short", timeZone: "UTC" });
// "Nov 30, 2026, 2:05 PM"
Pass timeZone when rendering on a server, or server and client output will differ and React will report a hydration mismatch.
formatRelativeTime(value, unit, opts?)
Formats a signed amount in a given unit. opts is { locales? } & Intl.RelativeTimeFormatOptions.
formatRelativeTime(-3, "day", { locales: "en" }); // "3 days ago"
formatRelativeTime(2, "hour", { locales: "es" }); // "dentro de 2 horas"
formatRelativeTime(-1, "day", { locales: "en", numeric: "auto" }); // "yesterday"
formatRelativeTimeFromDate(date, opts?)
Describes a date relative to now, choosing the largest unit that fits: seconds, minutes, hours, days, weeks, months or years. opts adds now?: Date so output can be made deterministic in tests and server rendering. It defaults to numeric: "auto", so you get “yesterday” rather than “1 day ago”.
const now = new Date("2026-11-30T12:00:00Z");
formatRelativeTimeFromDate(new Date("2026-11-30T11:55:00Z"), { locales: "en", now }); // "5 minutes ago"
formatRelativeTimeFromDate(new Date("2026-11-29T12:00:00Z"), { locales: "en", now }); // "yesterday"
formatRelativeTimeFromDate(new Date("2027-02-28T12:00:00Z"), { locales: "en", now }); // "in 3 months"
Months are approximated as 30 days and years as 365 days.
formatList(items, opts?)
Joins strings as a natural-language list. opts is { locales? } & Intl.ListFormatOptions.
formatList(["Ada", "Grace", "Linus"], { locales: "en" }); // "Ada, Grace, and Linus"
formatList(["Ada", "Grace", "Linus"], { locales: "es" }); // "Ada, Grace y Linus"
formatList(["PDF", "CSV"], { locales: "en", type: "disjunction" }); // "PDF or CSV"
clearFormatterCache()
Drops every cached Intl formatter. You should not need it in an application; it exists for tests and for long-running processes that format with an unbounded variety of options.
Formatting inside messages
Most of the time you do not call these directly. Numbers, currencies and dates that are part of a sentence belong in the message, where the translator can position them:
Your plan renews on {date, date, long} for {price, number, ::currency/EUR}.
Use the helpers for values that stand alone, such as a table cell or a chart label. In React, the standalone <Num>, <Currency>, <DateTime> and <RelativeTime> components wrap these helpers and read the locale from context.
Something wrong or unclear on this page? Open an issue.