|$ 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
◆JavaScript◆Intermediate
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
| 1 | const date = new Date("2024-03-15T12:00:00"); |
| 2 | |
| 3 | // Basic usage |
| 4 | new Intl.DateTimeFormat("en-US").format(date); |
| 5 | // "3/15/2024" |
| 6 | |
| 7 | new Intl.DateTimeFormat("de-DE").format(date); |
| 8 | // "15.3.2024" |
| 9 | |
| 10 | new Intl.DateTimeFormat("ja-JP").format(date); |
| 11 | // "2024/3/15" |
| 12 | |
| 13 | // Custom options |
| 14 | new 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) |
| 23 | const rtf = new Intl.RelativeTimeFormat("en", { numeric: "auto" }); |
| 24 | rtf.format(-1, "day"); // "yesterday" |
| 25 | rtf.format(3, "month"); // "in 3 months" |
Number & Currency Formatting
Intl.NumberFormat handles numbers, currencies, percentages, and units.
intl-number.js
JavaScript
| 1 | const nf = new Intl.NumberFormat("en-US"); |
| 2 | |
| 3 | nf.format(1234567.89); |
| 4 | // "1,234,567.89" |
| 5 | |
| 6 | // Currency |
| 7 | new Intl.NumberFormat("en-US", { |
| 8 | style: "currency", |
| 9 | currency: "USD", |
| 10 | }).format(42.50); |
| 11 | // "$42.50" |
| 12 | |
| 13 | new Intl.NumberFormat("de-DE", { |
| 14 | style: "currency", |
| 15 | currency: "EUR", |
| 16 | }).format(42.50); |
| 17 | // "42,50 €" |
| 18 | |
| 19 | // Units (ES2020) |
| 20 | new 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.