Dates, Numbers, and Internationalization

Intermediate · 10 min

`Date`'s well-known rough edges, why `0.1 + 0.2 !== 0.3`, and using `Intl` to format dates and numbers correctly instead of hand-rolling it.

Before thisValues, Types, and Coercion

Web Dev Simplified · 10 min

Dates, Numbers, and Internationalization

The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.

Taught by Web Dev SimplifiedReport a problem with this video
Not started
Set this lesson's status

What you'll learn

  • A `Date` object is mutable — calling a setter like `setDate()` changes the object in place rather than returning a new one, so passing a `Date` around and modifying it can silently change it everywhere else it's referenced too.
  • Months are zero-indexed (January is `0`, December is `11`) while days of the month are not (the 1st is `1`) — a mismatch that has caused an enormous number of off-by-one bugs in date code.
  • A `Date` is created and displayed relative to a timezone, and the same underlying timestamp will print differently depending on where it's rendered — code that silently assumes "the user's timezone" or "UTC" without checking which one it actually has will eventually be wrong for someone.
  • JavaScript numbers are IEEE-754 double-precision floats, which can't represent most decimal fractions exactly — that's why `0.1 + 0.2 === 0.3` is `false`: both operands are already tiny approximations before the addition even happens.
  • The `Intl` API (`Intl.DateTimeFormat`, `Intl.NumberFormat`, and related constructors) formats dates, numbers, and text correctly for a given locale using built-in, maintained rules — reaching for it beats hand-rolling locale-specific formatting, which is a deep enough problem that almost every manual attempt at it is subtly wrong somewhere.

Three different kinds of trouble, one lesson

This lesson groups three things that don’t look related at first: dates, numbers, and internationalization. They belong together because they’re all versions of the same lesson — JavaScript’s built-in representations of “obvious” everyday concepts have sharp edges that only show up once you hit them, and in every case, the fix is the same shape: know the edge exists, and reach for the built-in tool designed to handle it rather than patching around it yourself.

Date’s rough edges

Date looks simple and mostly behaves predictably, which is exactly what makes its exceptions dangerous. Three are worth knowing cold:

It’s mutable. someDate.setMonth(5) changes someDate in place and returns a number (a timestamp), not a new Date. If you handed that same Date object to three different parts of your program, all three now see the mutation. Treat a Date you don’t own like you’d treat any other mutable object someone else might still be holding — don’t change it under them.

Months are zero-indexed, days aren’t. new Date(2024, 0, 15) is January 15th — month 0 is January, month 11 is December, but the day argument counts normally from 1. There’s no deep reason for this beyond historical accident, and it has caused more off-by-one bugs than almost any other single API quirk in the language.

Timezones are implicit until you make them explicit. The same underlying timestamp renders as a different wall-clock time depending on what timezone it’s displayed in. Code that doesn’t think about which timezone it’s assuming — the server’s, the user’s, UTC — will work during testing and then be wrong for someone in production, usually right around midnight.

Why 0.1 + 0.2 isn’t 0.3

JavaScript has one numeric type for ordinary numbers, and it’s IEEE-754 double-precision floating point — the same representation most languages use for a “double.” That format stores numbers in binary, and most decimal fractions — including 0.1 and 0.2 — don’t have an exact binary representation, the same way 1/3 doesn’t have an exact decimal one. Each is stored as the closest approximation the format allows, and 0.1 + 0.2 adds two approximations together, landing on a result that’s extremely close to 0.3 but not bit-for-bit identical to it. This isn’t a bug — every language using the same floating-point standard has the identical behavior — it’s just a property of the representation you should expect and plan around (usually by comparing with a small tolerance instead of ===, or by working in integer cents instead of fractional currency amounts).

Intl: the tool that already solved formatting

Once you know dates and numbers have edges, the next question is how to actually display them correctly — and “correctly” depends on who’s looking. 10/12/2025 reads as October 12th in the US and December 10th in much of Europe. A price needs a currency symbol in the right place for the reader’s convention, not yours. Hand-writing this logic means encoding an enormous pile of locale-specific rules yourself, and getting some of them subtly wrong is close to guaranteed.

Intl is the built-in answer:

new Intl.DateTimeFormat("en-GB").format(new Date());   // "14/11/2026"
new Intl.DateTimeFormat("en-US").format(new Date());   // "11/14/2026"
new Intl.NumberFormat("en-US", {
  style: "currency",
  currency: "USD",
}).format(1234.5); // "$1,234.50"

The video demonstrates exactly this: using Intl to handle number and date formatting correctly instead of assembling it by hand, which is the practical skill this lesson is really pushing you toward. The Date and floating-point pitfalls above are the “why you’d get this wrong on your own” half; Intl is the “here’s the tool that’s already solved it” half.

Where this fits

This closes out the modules-and-utilities group. It leans on the type and coercion fundamentals from early in the roadmap — knowing that numbers are one type with one representation is what makes the floating-point behavior make sense rather than feel like a bug. From here, the last group in this roadmap moves into the corners of the language — symbols, proxies, memory — that separate knowing JavaScript’s syntax from understanding its model.

Check yourself

  1. 01A function takes a `Date` object, calls `.setMonth(0)` on it to "reset" it, and returns it. What's the risk in doing this if the caller still holds a reference to the original `Date` they passed in?

    `Date` objects are mutable, and `setMonth` changes the object in place rather than creating a new one. The caller's original reference now points to the same, already-modified object — so if they expected their original date to be untouched, it isn't. This is the same "shared mutable reference" hazard objects have in general, just easy to forget about because a date looks like a simple value.

  2. 02`new Date(2024, 0, 15)` creates a date for which month?

    January — the month argument to the `Date` constructor is zero-indexed, so `0` means January and `11` means December. This is inconsistent with the day-of-month argument, which is not zero-indexed (`15` means the 15th), and that inconsistency is a classic source of off-by-one date bugs.

  3. 03Why does `0.1 + 0.2 === 0.3` evaluate to `false` in JavaScript?

    JavaScript numbers are IEEE-754 double-precision floating point, which can only exactly represent a limited set of binary fractions. Neither `0.1` nor `0.2` has an exact binary representation, so each is already stored as a tiny approximation before any arithmetic happens — adding the two approximations together produces a value that's extremely close to, but not bit-for-bit equal to, the similarly-approximated `0.3`.

  4. 04You need to show a price as currency and a date in the format a visitor's own locale expects, without knowing in advance what locale that will be. What's the better approach: write locale-specific formatting logic yourself, or use `Intl`?

    Use `Intl` — `Intl.NumberFormat` and `Intl.DateTimeFormat` already encode the formatting rules (decimal separators, currency symbols and placement, date ordering, month names) for every locale they support, maintained against the actual standards. Hand-rolling this is a much bigger problem than it looks like at first, and almost every manual attempt gets some locale subtly wrong.

  5. 05What does `Intl.DateTimeFormat` actually solve that just calling `.toString()` on a `Date` doesn't?

    `.toString()` gives you one fixed, implementation-defined format that ignores the viewer's locale entirely. `Intl.DateTimeFormat` lets you specify a locale (or read the environment's default) and get a date formatted the way that locale actually expects it — different locales order day/month/year differently and use different separators, and `Intl` handles that correctly instead of assuming everyone reads dates the way you do.

Go deeper

Notes

Private to you

Esc
↑↓ navigate↵ open? shortcuts

Keyboard shortcuts

Press Esc to close

Anywhere

⌘K
Search and commands
/
Search
?
This sheet
Esc
Close

Reading a lesson

J
Next lesson
K
Previous lesson
B
Bookmark