Ask your question and get a summary of the document by referencing this page and the AI provider of your choice
The content of this page was translated using an AI.
See the last version of the original content in EnglishIf you have an idea for improving this documentation, please feel free to contribute by submitting a pull request on GitHub.
GitHub link to the documentationCopy doc Markdown to clipboard
Formatting dates and numbers by locale with Intl
Translating strings is the visible half of i18n. The half that generates bug reports is formatting: a German user seeing 1,234.56 instead of 1.234,56, a Japanese user seeing 08/02/2026 and reading it as August, or a date that renders differently on the server and the client and takes the page down on hydration.
None of that needs a library. Intl is in every runtime you target.
Table of Contents
Start by deleting your date helper
Almost every codebase has a formatDate written before anyone thought about locales. It hardcodes an order, a separator, and usually English month names.
Copy the code to the clipboard
Intl.DateTimeFormat replaces it and is correct in every locale:
Copy the code to the clipboard
The same applies to numbers. toFixed(2) produces 1234.56 everywhere, which is wrong in most of Europe.
What Intl covers
Open the table in a modal to view all data content clearly
| API | Use it for |
|---|---|
Intl.DateTimeFormat | Dates and times, with dateStyle / timeStyle presets |
Intl.NumberFormat | Decimals, currency, percent, units, compact notation |
Intl.RelativeTimeFormat | "3 days ago", "in 2 hours" |
Intl.ListFormat | "a, b, and c" versus "a, b et c" |
Intl.PluralRules | Which plural category a number falls into |
Intl.Collator | Sorting strings correctly per language |
Intl.Collator is the one people forget. array.sort() on strings uses code point order, so accented characters sort after z and Swedish ö lands in the wrong place. If you sort user-visible lists, sort with a collator.
Copy the code to the clipboard
Prefer presets to hand-built options
dateStyle and timeStyle let the locale decide the order and separators. Specifying year, month and day individually gives you control you usually should not want, because the correct order differs by locale and you are overriding CLDR data with your own assumption.
Copy the code to the clipboard
Use explicit components only when the design genuinely requires a fixed shape, for instance a table column that must stay narrow.
Constructing formatters is expensive
This is the performance detail that matters. Building an Intl.NumberFormat involves loading locale data, and it is far more expensive than the format() call that follows. Doing it inside a render or a loop over a thousand rows is a measurable cost.
Copy the code to the clipboard
toLocaleDateString() and toLocaleString() have the same problem hidden inside them: each call constructs a formatter. They are fine for one value and wrong for a list.
Cache by the combination of locale and options, since those are what define a formatter:
Copy the code to the clipboard
The timezone bug that only appears in production
This one costs entire afternoons. A server renders a date, the browser hydrates it, and React throws a hydration mismatch because the two produced different text.
The cause is that Intl.DateTimeFormat uses the ambient timezone when you do not name one. Your production server runs in UTC. Your laptop does not. So the bug is invisible locally and reproducible only in production, which is the worst possible combination.
Copy the code to the clipboard
Three workable approaches:
- Pin a timezone on the server and pass it explicitly. Correct and deterministic, but everyone sees UTC.
- Render on the client only, with a stable placeholder for the server pass. Correct per user, costs a flash.
- Store the user's timezone and pass it on both sides. Best result, most work.
Whichever you pick, always pass timeZone explicitly for any date rendered on both server and client. A date with no timezone is a date with two values.
Currency needs a currency, not a locale
Locale and currency are independent. fr-FR does not mean euros: a French user can be looking at a USD invoice.
Copy the code to the clipboard
The locale controls the separators, digit grouping and symbol placement. The currency comes from your data. Deriving one from the other is a bug that reaches accounting.
Also note currencyDisplay. In an interface where several currencies coexist, "code" removes the ambiguity between US, Canadian and Australian dollars.
Relative time reads better than absolute time
For anything recent, "2 hours ago" beats a timestamp, and Intl.RelativeTimeFormat localises it properly.
Copy the code to the clipboard
numeric: "auto" is what produces "yesterday" instead of "1 day ago". Without it you get the numeric form in every language, which reads like a machine.
What Intlayer adds
Intlayer wraps these in cached helpers so you do not maintain the map above, and so the active locale is applied by default rather than passed at every call site.
Copy the code to the clipboard
date() also accepts presets ("short", "long", "dateOnly", "timeOnly", "full") so the common cases do not need an options object. React and Vue equivalents exist as hooks and composables, which resolve the active locale from context instead of taking it as an argument.
To be clear about what this is: a caching layer and a locale default over the platform API. The formatting behaviour is Intl, and everything in this post applies whether or not you use it. Full signatures in the formatters documentation.
Common mistakes
toLocaleDateString()with no locale. Uses the runtime's locale, which on a server is whatever the container was configured with.- Formatting in a loop. Constructing the formatter dominates the cost. Build once.
- No
timeZoneon isomorphic dates. Hydration mismatch that never reproduces on your machine. - Deriving currency from locale.
fr-FRis not euros. sort()on user-visible strings. UseIntl.Collator.- Hardcoding month or day names. They are in CLDR already, in every language.
numeric: "always"for relative time. "1 day ago" where every language has a word for yesterday.
Going further
- Formatters and locale utilities:
number,currency,date,relativeTime,list - Configuration reference
- Benchmark reports across frameworks
- Drop-in react-intl compat adapter
- ICU message format: plurals, select and number skeletons
- How to test translations, including formatter and plural coverage
- What internationalisation actually covers
Comments
No comments yet. Be the first to share your thoughts.
