|$ curl https://forge-ai.dev/api/markdown?path=docs/js/intl
$cat docs/javascript-intl-api.md
updated Recently·18 min read·published

JavaScript Intl API

JavaScriptIntermediate
Introduction

The Intl namespace provides language-sensitive formatting and comparison — dates, times, numbers, currencies, plurals, relative time, and list formatting — with automatic locale-aware output.

Date & Time Formatting

Intl.DateTimeFormat formats dates according to locale conventions.

intl-datetime.js
JavaScript
1const date = new Date("2024-03-15T12:00:00");
2
3// Basic usage
4new Intl.DateTimeFormat("en-US").format(date);
5// "3/15/2024"
6
7new Intl.DateTimeFormat("de-DE").format(date);
8// "15.3.2024"
9
10new Intl.DateTimeFormat("ja-JP").format(date);
11// "2024/3/15"
12
13// Custom options
14new Intl.DateTimeFormat("en-US", {
15 weekday: "long",
16 year: "numeric",
17 month: "long",
18 day: "numeric",
19}).format(date);
20// "Friday, March 15, 2024"
21
22// Relative time (ES2020)
23const rtf = new Intl.RelativeTimeFormat("en", { numeric: "auto" });
24rtf.format(-1, "day"); // "yesterday"
25rtf.format(3, "month"); // "in 3 months"
Number & Currency Formatting

Intl.NumberFormat handles numbers, currencies, percentages, and units.

intl-number.js
JavaScript
1const nf = new Intl.NumberFormat("en-US");
2
3nf.format(1234567.89);
4// "1,234,567.89"
5
6// Currency
7new Intl.NumberFormat("en-US", {
8 style: "currency",
9 currency: "USD",
10}).format(42.50);
11// "$42.50"
12
13new Intl.NumberFormat("de-DE", {
14 style: "currency",
15 currency: "EUR",
16}).format(42.50);
17// "42,50 €"
18
19// Units (ES2020)
20new Intl.NumberFormat("en-US", {
21 style: "unit",
22 unit: "kilometer-per-hour",
23}).format(100);
24// "100 km/h"

info

Always use Intl for locale-sensitive formatting instead of manual string concatenation. It handles thousands separators, decimal symbols, currency placement, and date order automatically based on the user's locale.