Skip to content
jshacks.dev
Fundamentals

The Intl API in JavaScript for Numbers Dates and Lists

The Intl API formats numbers, dates, lists, plurals and relative times for any locale without a library. See the formatters, options and why to reuse them.

9 min read
Summarize in AI
Flat illustration of a browser and a server trading a request and a response, with the JavaScript, Swagger and NGINX logos.

Most JavaScript programs print numbers and dates, and most reach for toFixed(2), string concatenation and a date library to do it. The language already ships a complete internationalization toolkit that handles separators, currency symbols, date order, plural rules and list conjunctions for any locale, and it costs nothing in bundle size. This post tours the formatters you will use daily, with output from Node.js 24 using fixed locales so the results are stable, and ends on the habits that keep them fast and testable. The number rounding that toFixed gets wrong is covered in the post on 0.1 plus 0.2, and dates in the post on Date pitfalls.

Numbers

Intl.NumberFormat takes a locale and options and returns a formatter. The same number prints differently by locale, with grouping and decimal marks in different places.

JavaScript
const number = 1234567.891;

for (const locale of ["en-US", "de-DE", "en-IN", "fr-CH"]) {
  const text = new Intl.NumberFormat(locale).format(number);
  console.log(locale.padEnd(6), JSON.stringify(text.replace(/[\u00a0\u202f]/g, "_")));
}

const percent = new Intl.NumberFormat("en-US", { style: "percent", maximumFractionDigits: 1 });
const compact = new Intl.NumberFormat("en-US", { notation: "compact", maximumFractionDigits: 1 });
const unit = new Intl.NumberFormat("en-US", { style: "unit", unit: "kilometer-per-hour" });
const fixed = new Intl.NumberFormat("en-US", { minimumFractionDigits: 2, maximumFractionDigits: 2 });

console.log(percent.format(0.256), compact.format(1250000), unit.format(88), fixed.format(5));
Text
en-US  "1,234,567.891"
de-DE  "1.234.567,891"
en-IN  "12,34,567.891"
fr-CH  "1_234_567,891"
25.6% 1.3M 88 km/h 5.00

Each locale used its own grouping and decimal marks. Swiss French groups digits with a special narrow no-break space, which the code swaps for a visible placeholder so you can see where it sits. Percent, compact notation and units are options on the same class. Rounding is controlled with the fraction digit options, and fixed shows the standard replacement for toFixed(2), which also respects the locale.

Currency

Currency formatting needs the currency code, since the symbol and position depend on it and on the locale.

JavaScript
const amount = 1234.5;

const usd = new Intl.NumberFormat("en-US", { style: "currency", currency: "USD" });
const eur = new Intl.NumberFormat("de-DE", { style: "currency", currency: "EUR" });
const yen = new Intl.NumberFormat("ja-JP", { style: "currency", currency: "JPY" });
const code = new Intl.NumberFormat("en-US", { style: "currency", currency: "EUR", currencyDisplay: "code" });

console.log(usd.format(amount));
console.log(eur.format(amount).replaceAll(" ", " "));
console.log(yen.format(amount));
console.log(code.format(amount).replaceAll(" ", " "));
console.log(usd.format(-5), usd.format(0.005), usd.format(0.015));
console.log(usd.resolvedOptions().maximumFractionDigits, yen.resolvedOptions().maximumFractionDigits);
Text
$1,234.50
1.234,50 €
¥1,235
EUR 1,234.50
-$5.00 $0.01 $0.02
2 0

The euro in German puts the symbol after the number, with a space, and the yen has no decimals, because the currency's minor unit is zero. The formatter chose the digits from the currency, which hand-written code usually forgets. Half-cent values round using the library's rounding mode, which is half-expand by default, so 0.005 rounds up. These are display rules and not accounting rules: keep money as integers, as in the post on floating point, and format only at the edge.

Dates and times

Intl.DateTimeFormat formats a Date or a timestamp. The timeZone option decides which wall clock to show, and the style options pick a preset.

JavaScript
const instant = new Date(Date.UTC(2026, 4, 6, 13, 5, 9));

console.log(new Intl.DateTimeFormat("en-US", { dateStyle: "long", timeZone: "UTC" }).format(instant));
console.log(new Intl.DateTimeFormat("en-GB", { dateStyle: "full", timeZone: "UTC" }).format(instant));
console.log(new Intl.DateTimeFormat("en-US", { dateStyle: "short", timeStyle: "short", timeZone: "Asia/Tokyo" }).format(instant));
console.log(new Intl.DateTimeFormat("en-US", { hour: "numeric", minute: "2-digit", hour12: false, timeZone: "America/New_York" }).format(instant));
console.log(new Intl.DateTimeFormat("en-US", { month: "short", day: "numeric", weekday: "long", timeZone: "UTC" }).format(instant));
console.log(new Intl.DateTimeFormat("de-DE", { dateStyle: "medium", timeZone: "UTC" }).format(instant));
Text
May 6, 2026
Wednesday, 6 May 2026
5/6/26, 10:05 PM
09:05
Wednesday, May 6
06.05.2026

One instant read as 13:05 in UTC, 22:05 in Tokyo and 09:05 in New York. The timeZone option is the fix for the "date off by a day" bugs: format a calendar date with timeZone: "UTC" and it will not shift. The preset styles give a consistent length per locale, and the component options give finer control. The Temporal post shows toLocaleString() on the newer types, which uses this class underneath.

Lists

Intl.ListFormat joins items with the right conjunction and punctuation for the language.

JavaScript
const items = ["apples", "pears", "plums"];

const and = new Intl.ListFormat("en-US", { style: "long", type: "conjunction" });
const or = new Intl.ListFormat("en-US", { style: "long", type: "disjunction" });
const short = new Intl.ListFormat("en-GB", { style: "short", type: "unit" });
const german = new Intl.ListFormat("de-DE", { type: "conjunction" });

console.log(and.format(items));
console.log(or.format(items.slice(0, 2)));
console.log(short.format(items));
console.log(german.format(["Äpfel", "Birnen", "Pflaumen"]));
console.log(and.format(["only"]), and.format([]) === "");
Text
apples, pears, and plums
apples or pears
apples, pears, plums
Äpfel, Birnen und Pflaumen
only true

English gets the serial comma and the "and", the disjunction gets "or", and German uses "und" with no comma before it. The one-element and empty lists work without special cases. A hand-built items.join(", ") gets none of this, and breaks on translation.

Relative time and plurals

Intl.RelativeTimeFormat writes "in 3 days" and "2 hours ago" from a number and a unit. Intl.PluralRules tells you which plural category a number falls in, and the categories differ widely between languages.

JavaScript
const rtf = new Intl.RelativeTimeFormat("en", { numeric: "auto" });

console.log(rtf.format(-1, "day"), "|", rtf.format(1, "day"), "|", rtf.format(0, "day"));
console.log(rtf.format(3, "week"), "|", rtf.format(-2, "hour"), "|", rtf.format(-5, "minute"));
console.log(new Intl.RelativeTimeFormat("es", { numeric: "auto" }).format(-1, "day"));

const plurals = new Intl.PluralRules("en");
const polish = new Intl.PluralRules("pl");
const ordinals = new Intl.PluralRules("en", { type: "ordinal" });

console.log([0, 1, 2].map((n) => plurals.select(n)));
console.log([1, 2, 5, 22].map((n) => polish.select(n)));

const suffix = { one: "st", two: "nd", few: "rd", other: "th" };
console.log([1, 2, 3, 4, 11, 21, 22].map((n) => n + suffix[ordinals.select(n)]).join(" "));
Text
yesterday | tomorrow | today
in 3 weeks | 2 hours ago | 5 minutes ago
ayer
[ 'other', 'one', 'other' ]
[ 'one', 'few', 'many', 'few' ]
1st 2nd 3rd 4th 11th 21st 22nd

The numeric: "auto" option replaced "in 1 day" with "tomorrow" and "1 day ago" with "yesterday", and Spanish did the same in its own words. English has two plural categories for cardinals, and Polish has more, so code that checks n === 1 is wrong for it. The ordinal rules gave the right English suffixes for 11, 21 and 22, which a modulo-10 rule gets wrong for 11. Message libraries use PluralRules to pick the right translation string, and you can use it directly for simple cases.

Sorting and comparing strings

Intl.Collator compares strings by language rules and is what localeCompare() uses underneath. It also offers numeric ordering and case options.

JavaScript
const words = ["z", "ä", "a", "Z", "A"];

console.log([...words].sort());
console.log([...words].sort(new Intl.Collator("de").compare));
console.log([...words].sort(new Intl.Collator("sv").compare));

const files = ["file10", "file2", "file1"];
console.log([...files].sort());
console.log([...files].sort(new Intl.Collator(undefined, { numeric: true }).compare));
console.log(new Intl.Collator("en", { sensitivity: "base" }).compare("a", "A"));
Text
[ 'A', 'Z', 'a', 'z', 'ä' ]
[ 'a', 'A', 'ä', 'z', 'Z' ]
[ 'a', 'A', 'z', 'Z', 'ä' ]
[ 'file1', 'file10', 'file2' ]
[ 'file1', 'file2', 'file10' ]
0

The default sort puts uppercase first and ä last, by code unit. German places ä next to a, Swedish places it after z, and the numeric option sorts file names the way people expect. The sensitivity: "base" option treats a and A as equal, which is useful for case-insensitive matching.

formatToParts and reuse

formatToParts() returns the output as labeled pieces, which lets you style a currency symbol or hide a unit without parsing a string. And because constructing a formatter loads locale data, you should build one and call format() many times.

JavaScript
const money = new Intl.NumberFormat("en-US", { style: "currency", currency: "USD" });

const parts = money.formatToParts(1234.5);
console.log(parts.map((p) => `${p.type}:${p.value}`).join(" "));

const html = parts
  .map((p) => (p.type === "currency" ? `<sup>${p.value}</sup>` : p.value))
  .join("");
console.log(html);

const prices = [5, 12.5, 1999];
console.log(prices.map((n) => money.format(n)));
console.log(money.resolvedOptions().locale, Intl.NumberFormat.supportedLocalesOf(["en-US", "xx-XX"]));
Text
currency:$ integer:1 group:, integer:234 decimal:. fraction:50
<sup>$</sup>1,234.50
[ '$5.00', '$12.50', '$1,999.00' ]
en-US [ 'en-US' ]

The parts are typed, so the symbol was wrapped in markup while the digits stayed plain. One formatter formatted all three prices. resolvedOptions() shows what the runtime actually used, and supportedLocalesOf() filters a list to the locales it knows. If you create a formatter inside a render function or a loop, hoist it to module scope or memoize it.

Putting several formatters together

A realistic message uses more than one formatter, and building the pieces once makes the code short. An order summary needs a currency, a count with the right plural, a date and a list.

JavaScript
const locale = "en-US";
const money = new Intl.NumberFormat(locale, { style: "currency", currency: "USD" });
const plural = new Intl.PluralRules(locale);
const list = new Intl.ListFormat(locale, { type: "conjunction" });
const day = new Intl.DateTimeFormat(locale, { dateStyle: "medium", timeZone: "UTC" });

const noun = { one: "item", other: "items" };

function summary(order) {
  const count = order.items.length;
  const names = list.format(order.items.map((item) => item.name));
  const total = money.format(order.items.reduce((sum, item) => sum + item.cents, 0) / 100);
  return `${count} ${noun[plural.select(count)]} (${names}), ${total}, placed ${day.format(order.placed)}`;
}

const placed = new Date(Date.UTC(2026, 4, 6));

console.log(summary({ placed, items: [{ name: "pen", cents: 250 }] }));
console.log(summary({ placed, items: [{ name: "pen", cents: 250 }, { name: "ink", cents: 1299 }, { name: "pad", cents: 800 }] }));
Text
1 item (pen), $2.50, placed May 6, 2026
3 items (pen, ink, and pad), $23.49, placed May 6, 2026

The message adapts to one item and to several, with the plural noun, the serial comma and the total all handled by the formatters. Switching the locale constant to another language changes the number format, date order and list conjunction in one place, while the sentence template remains English, which is where a message library such as ICU MessageFormat takes over for full translation. The conversion from cents to dollars happens once, at the edge.

Testing formatted output

Tests that compare formatted strings are brittle when data updates change spacing or abbreviations. Pin the locale, compare against the parts when you can, and normalize whitespace. For currency, formatToParts() gives the digits and symbol separately, so a test can assert the number and the symbol without depending on the space between them. For dates, assert on an ISO string you formatted from, and leave the display format to a smaller set of snapshot-style checks that you update deliberately when the runtime's data changes.

Where Intl needs care

Output depends on the runtime's locale data, so the same call can differ between Node.js versions and browsers, and a test that hard-codes a formatted string can break when data updates. Test with fixed locales and a tolerance for spacing, as we did here. Do not parse formatted output back into numbers or dates, since the format is for people. Keep a canonical value, an integer or an ISO string, and format on the way out.

Browsers and Node.js 24 ship full locale data, while some minimal builds ship only English, in which case the formatter falls back silently. Check supportedLocalesOf() if your users depend on a specific locale.

The API is defined in the ECMAScript Internationalization specification, ECMA-402, and MDN's Intl reference documents each formatter and its options.

Also inAPIs
Share

Keep reading

Get new posts by email

We send one email when a new post is published. No spam.

Unsubscribe in one click. No spam, ever.