JavaScript Intermediate Language Basics · 7 min read

Intl for Numbers and Dates

Lesson 25 of 38 in the JavaScript learning path.

How do I format a price and a date for a locale?

Intl.NumberFormat and Intl.DateTimeFormat format values for a locale without hand-built strings. A currency format needs the style and the ISO currency code. A zero stays zero. Date styles such as medium take the month and day order from the locale, and timeZone decides which clock you show. The Date object itself stays an absolute instant.

Pass the locale explicitly. The runtime default follows the visitor's environment, so the same price can look different on two machines if you omit it. ListFormat joins names, and RelativeTimeFormat builds a phrase such as "in 2 days". These formatters do not translate your lesson titles. They format numbers, dates, lists, and relative times.

Syntax & Parameters

Syntax
new Intl.NumberFormat("en-PH", { style: "currency", currency: "PHP" }).format(price)

Example

Interactive preview available
app.js
const price = 1299.5;
const seats = 0;
const when = new Date("2026-11-30T09:30:00Z");

const peso = new Intl.NumberFormat("en-PH", {
  style: "currency",
  currency: "PHP",
  minimumFractionDigits: 2,
  maximumFractionDigits: 2
});

const dollar = new Intl.NumberFormat("en-US", {
  style: "currency",
  currency: "USD",
  minimumFractionDigits: 2,
  maximumFractionDigits: 2
});

const count = new Intl.NumberFormat("en-PH", {
  maximumFractionDigits: 0
});

const day = new Intl.DateTimeFormat("en-PH", {
  dateStyle: "medium",
  timeStyle: "short",
  timeZone: "Asia/Manila"
});

const list = new Intl.ListFormat("en", {
  style: "long",
  type: "conjunction"
});

const relative = new Intl.RelativeTimeFormat("en", {
  numeric: "auto"
});

const rows = [
  ["Price (en-PH)", peso.format(price)],
  ["Price (en-US)", dollar.format(price)],
  ["Seats", count.format(seats)],
  ["Starts", day.format(when)],
  ["Tracks", list.format(["PHP", "JavaScript", "SQL"])]
];

const output = document.createElement("ul");
output.id = "formats";
for (const row of rows) {
  const item = document.createElement("li");
  item.textContent = row[0] + ": " + row[1];
  output.append(item);
}

const hint = document.createElement("p");
hint.id = "relative-time";
hint.textContent = "Enrollment " + relative.format(2, "day");

document.body.append(output, hint);

Expected Output

Price (en-PH): ₱1,299.50
Price (en-US): $1,299.50
Seats: 0
Starts: Nov 30, 2026, 5:30 PM
Tracks: PHP, JavaScript, and SQL
Enrollment in 2 days

Line-by-Line Walkthrough

  1. price and seats are numbers. when is an absolute instant parsed from a UTC timestamp.
  2. The peso formatter pins locale en-PH, style currency, and currency PHP, with two fraction digits.
  3. The dollar formatter formats the same number for en-US and USD so the locales can be compared.
  4. The count formatter is a decimal format. format(0) stays 0 and does not become a currency.
  5. The date formatter uses en-PH, a medium date, a short time, and the Asia/Manila time zone.
  6. Manila is eight hours ahead of UTC, so 09:30 UTC is 17:30 local, which this locale prints as 5:30 PM.
  7. ListFormat joins the three track names with a conjunction in English.
  8. RelativeTimeFormat turns a numeric offset of 2 days into a phrase. numeric auto allows words when the locale has them.
  9. Each list item is label text plus the formatted string, assigned with textContent.
  10. None of these calls change price or when. Formatting is a view of the stored value.

Common Pitfalls & Security

Intl output depends on the locale data in the runtime. Pin the locale and the options, and do not assert one exact punctuation unless you also pin the environment.

  • Omitting the locale uses the visitor default, so a server render and a browser can disagree.
  • currency without style currency is ignored. You need both style and the ISO currency code.
  • A Date is an instant. Forgetting timeZone shows the runtime zone, not Manila.
  • dateStyle cannot be combined with an explicit month or day option. Choose one style of options.
  • Number formatters do not convert currency. They only present the number you already computed in that currency.
  • Do not build HTML with the formatted string and innerHTML. A locale symbol is still text. Use textContent.

Best Practices & Modern Standards

Create the formatter once and call format many times. Construction looks up locale data. Reuse the same NumberFormat for every price in a list. Pass a locale tag such as en-PH rather than a language name.

Store money as a number of currency units you control, or as integer minor units, and format at the edge of the page. Store instants in UTC. Apply the time zone only in the formatter that the visitor sees.

Real-World Use

The checkout card shows the course price as ₱1,299.50 for en-PH, keeps a seat count of 0 visible, and shows the start as Nov 30, 2026, 5:30 PM in Manila. The stored instant remains 2026-11-30T09:30:00Z.

Frequently Asked Questions

Which locale is used when I omit it?

The runtime default. Pass a tag such as en-PH when the page has a chosen locale, or the same data can format differently for two visitors.

Does formatting change the Date value?

No. format returns a string. The Date object stays the same instant. timeZone only affects the string.

Why does a zero price need care?

A currency formatter will print a real zero. Do not pre-check with a truthiness test that treats 0 as missing before you call format.

Can Intl translate the lesson title?

No. It formats numbers, dates, lists, and relative times. Title strings still come from your own copy for that locale.

Need more coins?

You do not have enough coins for this action.

Your balance: 0 coins

Share

Reactions

Loading reactions…