JavaScript i18n (Intl.*): Formatting Dates, Numbers, and Text
JavaScript's Intl APIs let you format dates, numbers, currency, lists, text comparisons, and relative times in a way that matches a user's locale. This matters any time your app serves people in different regions, because formatting rules vary by language, country, and writing system.
Quick answer: Use the Intl namespace when you need locale-aware formatting in JavaScript. The most common tools are Intl.DateTimeFormat, Intl.NumberFormat, and Intl.Collator, plus convenience methods like toLocaleString().
Difficulty: Beginner
You'll understand this better if you know: basic JavaScript objects, how dates and numbers are represented, and how function options objects work.
1. What Is JavaScript i18n (Intl.*)?
i18n is short for internationalization, and in JavaScript it usually refers to the built-in Intl APIs. These APIs help you display data in the format that a user's locale expects instead of using one fixed format for everyone.
- Intl is a built-in JavaScript namespace for locale-aware formatting and comparison.
- It supports dates, times, numbers, currencies, lists, relative time, plural rules, and text collation.
- It helps avoid hard-coded formatting like MM/DD/YYYY or 1,234.56.
- It works with locale tags such as en-US, fr-FR, and ja-JP.
At a high level, Intl answers questions like: What date format should I show? Which decimal separator should I use? How should I sort strings in this language?
2. Why JavaScript i18n (Intl.*) Matters
Many apps are used by people who do not share the same date, time, or number conventions. If you format everything using one culture's rules, users may misunderstand prices, deadlines, or names.
Intl matters because it lets you:
- show dates in the user's familiar style,
- display currency and percentages correctly,
- sort text in a language-aware way,
- present relative time such as "2 hours ago" or "in 3 days",
- avoid manual formatting code that is hard to maintain.
It is especially useful in dashboards, ecommerce sites, calendars, analytics tools, and any product with global users. If your app only targets one locale, you may still use it to keep formatting consistent and robust.
3. Core Strengths and Design Goals
The main goal of Intl is to separate data from presentation. You store a date or number once, then format it differently depending on the locale and options.
- Locale awareness: output matches regional expectations.
- Consistency: the same options produce predictable results.
- Flexibility: one API can format many styles.
- Performance: formatter objects can be reused instead of rebuilt repeatedly.
- Standardization: the API follows ECMAScript internationalization standards.
Unlike manual string building, Intl knows about things like numbering systems, calendars, and grammatical differences that are difficult to reproduce reliably by hand.
4. Where JavaScript i18n (Intl.*) Fits in the Ecosystem
Intl is part of the JavaScript runtime, not a separate package. In browsers, it is available in modern engines. In Node.js, it is also built in, though the available locale data can vary depending on how Node was built.
- Front-end: format UI labels, tables, prices, timestamps, and search results.
- Back-end: generate localized emails, PDFs, logs, and server-rendered pages.
- Cross-platform: keep formatting logic consistent between browser and server.
It often complements other APIs such as Date, Array.prototype.sort(), and String.prototype.localeCompare(). When you need translation of full sentences, you usually need a separate message library or localization workflow; Intl is mainly for formatting and comparison.
5. Key Features at a Glance
| API | What it does | Common use |
|---|---|---|
| Intl.DateTimeFormat | Formats dates and times | Calendars, timestamps, schedules |
| Intl.NumberFormat | Formats numbers, currency, percent | Prices, metrics, percentages |
| Intl.Collator | Compares strings using locale rules | Sorting names, search lists |
| Intl.RelativeTimeFormat | Formats relative times | "Yesterday", "in 5 minutes" |
| Intl.PluralRules | Chooses plural categories | Plural-sensitive labels |
| Intl.ListFormat | Formats lists naturally | "A, B, and C" |
These APIs are separate because each task has its own rules. Date formatting is not the same as number formatting, and string sorting is not the same as string equality.
6. How JavaScript i18n (Intl.*) Compares to Alternatives
| Approach | Strengths | Weaknesses |
|---|---|---|
| Intl APIs | Built in, locale-aware, standard, widely supported | Does not translate full UI text by itself |
| Manual formatting | Full control for a single fixed format | Hard to maintain, locale bugs, repeated code |
| Third-party i18n libraries | Great for message translation and advanced workflows | Extra dependency, often still uses Intl underneath |
Dates and numbers vs manual strings
Manual formatting can be fine for prototypes, but it quickly becomes fragile when users expect regional formats. For example, a hard-coded date string may be readable to you but confusing to someone in another locale.
Intl vs translation libraries
Translation libraries usually handle message catalogs, plural text, interpolation, and route switching. Intl focuses on the formatting layer. In many apps, you use both: translation for words, Intl for dates and numbers.
7. Common Misconceptions
Beginners often assume internationalization is only about translating text, but formatting is just as important. The following misconceptions come up frequently:
- "toLocaleString() is enough for everything." It is useful, but dedicated formatters give you more control and better readability.
- "Locale formatting is just cosmetic." In many apps, it changes meaning, especially for dates, currency, and large numbers.
- "Sorting strings alphabetically is always correct." Different languages have different collation rules, accents, and case behavior.
- "I only need one locale because my browser has one language." Your users may not share your browser language, and server output may differ from the client.
- "Intl translates UI text." It does not replace a translation system for labels and sentences.
8. Who Uses JavaScript i18n (Intl.*) and For What
- Product teams use it to display timestamps, money, and metrics in user-facing dashboards.
- Ecommerce sites use it for prices, discounts, shipping dates, and tax-related formatting.
- Scheduling apps use it for calendars, relative times, and time zone-sensitive displays.
- Content platforms use it for dates, views, rankings, and locale-aware search results.
- Backend services use it when generating emails, reports, or PDFs for multiple regions.
If your product has users outside a single country, you will usually benefit from using Intl somewhere in the stack.
9. Typical Learning Path
A good path for learning Intl is to start with the most visible formatting tasks, then move into comparison and advanced locale behavior.
- Learn Intl.NumberFormat and Intl.DateTimeFormat.
- Practice formatting currency, percentages, and relative time.
- Use Intl.Collator and localeCompare() for sorting.
- Explore Intl.ListFormat and Intl.PluralRules.
- Study time zones, calendars, and server/client consistency.
Once you are comfortable with those pieces, you can design a full localization strategy instead of formatting values one by one.
10. Key Points
- Intl is JavaScript's built-in internationalization toolkit.
- It formats data according to locale-specific rules.
- It is best for dates, numbers, currency, lists, relative time, and string comparison.
- It improves correctness, readability, and user experience across regions.
- It does not replace translation tools for full sentences and labels.
11. Next Steps
- Try Intl.DateTimeFormat with two or three locales and compare the output.
- Format a currency value with Intl.NumberFormat and the currency option.
- Sort a list of names with Intl.Collator to see how locale rules change ordering.
- Check your browser and server environments to make sure they support the locales you need.
- Combine Intl formatting with a translation system if your UI needs multilingual text.
12. Final Summary
JavaScript's Intl APIs are the standard way to format values for different locales. They solve a real problem: the same number, date, or string may need to look different depending on the user's language and region.
For most developers, the best starting points are Intl.DateTimeFormat and Intl.NumberFormat. From there, you can add string collation, relative time, and plural handling as your app grows.
Use Intl whenever formatting needs to be correct, user-friendly, and locale-aware. If you also need translated UI copy, pair it with a dedicated localization workflow so your app handles both formatting and language well.