Lesson 5 of 9 · 25m
Dates, Numbers, Currency, and Units
Format values through locale-aware libraries while keeping time zone, currency, calendar, and precision as separate semantic decisions.
A date string is not a representation of truth; it is a rendering decision. The truth is usually an instant, a calendar date, a duration, or a quantity. Teams get into trouble when they store 03/04/2025 and later discover nobody knows whether that is March 4 or April 3. Separate data semantics from presentation, then let locale-aware formatters do their job.
Use platform internationalization APIs
JavaScript's Intl API covers the common cases:
const date = new Date("2026-11-30T18:30:00Z");
const opts = { dateStyle: "medium", timeStyle: "short", timeZone: "America/New_York" };
new Intl.DateTimeFormat("en-US", opts).format(date); // "Nov 30, 2026, 1:30 PM"
new Intl.DateTimeFormat("de-DE", opts).format(date); // "30.11.2026, 13:30"The same instant renders differently by locale and time zone. Neither output is more correct; they answer different presentation contracts.
Time zone is not locale
en-US does not imply America/New_York, and fr-FR does not imply Europe/Paris. Users travel, servers run in UTC, and distributed teams look at shared events. Store instants in UTC using a precise type such as RFC 3339 or epoch milliseconds, then render in an explicit user, organization, or event time zone.
Calendar dates are different again. A birthday 1992-04-18 is not an instant at midnight UTC. Converting it to a timestamp can move it to the previous day in some zones. Keep date-only semantics date-only until presentation.
Calendars and numbering systems are locale data
Not every user expects the Gregorian calendar or Latin digits. Unicode extensions such as ar-SA-u-ca-islamic and ar-EG-u-nu-arab select non-default calendar or numbering systems; use them when users expect them and test input, fonts, and screen readers end to end.
Currency is a semantic property of the amount
Locale never determines currency. A French user can hold a USD invoice, and a US user can donate in EUR:
new Intl.NumberFormat("fr-FR", { style: "currency", currency: "USD" }).format(1234.5);
// "1 234,50 $US"Store currency as an ISO 4217 code alongside the amount. Beware integer minor units: Stripe and many payment APIs treat 1299 as 12.99 in a zero-decimal-aware currency model, while some currencies such as JPY have no minor unit. Locale formatting does not rescue incorrect monetary representation.
Units, percent, compact numbers, and ranges
Use formatters rather than hard-coding symbols and order:
new Intl.NumberFormat("en-IN", { notation: "compact" }).format(1250000);
new Intl.NumberFormat("de-DE", { style: "unit", unit: "kilometer-per-hour" }).format(88);
new Intl.NumberFormat("fr-FR", { style: "percent" }).format(0.482);Spacing, grouping, decimal separators, digit shape, and non-breaking spaces are all presentation details. Copying output back into parsing logic is how localized input breaks.
Field notes
- Never parse localized numeric input with
parseFloatalone if users may type grouping or decimal conventions. toLocaleString()without an explicit locale is environment-dependent. Server output can differ by container image.- Non-breaking spaces in formatted currency are real characters. Snapshot tests should treat formatter output as data-dependent.
- ICU and CLDR versions vary across browsers, Node, and mobile platforms. Pin or polyfill when consistency matters.
Was this lesson helpful?