Relative Time and Plurals
Use RelativeTimeFormat and PluralRules.
Relative Time and Plurals is a free JavaScript Academy lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the JavaScript Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Intl.RelativeTimeFormat
Intl.RelativeTimeFormat produces phrases like "3 days ago" or "in 2 hours" in the user's language.
const rtf = new Intl.RelativeTimeFormat("en-US");
console.log(rtf.format(-1, "day")); // "1 day ago"
console.log(rtf.format(2, "hour")); // "in 2 hours"Negative for Past, Positive for Future
The sign of the number sets the direction: negative is past, positive is future.
const rtf = new Intl.RelativeTimeFormat("en-US");
console.log(rtf.format(-3, "week")); // "3 weeks ago"
console.log(rtf.format(5, "minute")); // "in 5 minutes"The numeric Option
numeric: "auto" uses idiomatic words like "yesterday" or "tomorrow" instead of "1 day ago".
const rtf = new Intl.RelativeTimeFormat("en-US", { numeric: "auto" });
console.log(rtf.format(-1, "day")); // "yesterday"
console.log(rtf.format(0, "day")); // "today"Localized Relative Time
Switching the locale translates the whole phrase automatically.
console.log(new Intl.RelativeTimeFormat("es").format(-2, "day")); // "hace 2 dias"
console.log(new Intl.RelativeTimeFormat("fr").format(3, "hour")); // "dans 3 heures"Choosing the Right Unit
Real apps pick the largest sensible unit. Compute the difference in seconds and step down through units.
function ago(date) {
const diff = (date - Date.now()) / 1000;
const rtf = new Intl.RelativeTimeFormat("en-US", { numeric: "auto" });
if (Math.abs(diff) < 60) return rtf.format(Math.round(diff), "second");
if (Math.abs(diff) < 3600) return rtf.format(Math.round(diff / 60), "minute");
return rtf.format(Math.round(diff / 3600), "hour");
}
console.log(ago(new Date(Date.now() - 90000)));Intl.PluralRules
Pluralization is more than adding "s". Intl.PluralRules maps a number to a category like "one" or "other" per language.
const pr = new Intl.PluralRules("en-US");
console.log(pr.select(1)); // "one"
console.log(pr.select(2)); // "other"
console.log(pr.select(0)); // "other"Building Plural Messages
Use the category to pick the correct word.
const pr = new Intl.PluralRules("en-US");
const forms = { one: "item", other: "items" };
function label(n) { return n + " " + forms[pr.select(n)]; }
console.log(label(1)); // "1 item"
console.log(label(5)); // "5 items"Languages with More Forms
Many languages have several plural categories. Polish, for example, distinguishes "few" and "many" — hardcoded English logic would be wrong.
const pr = new Intl.PluralRules("pl");
console.log(pr.select(1)); // "one"
console.log(pr.select(3)); // "few"
console.log(pr.select(5)); // "many"Ordinal Plurals
Pass type: "ordinal" to get categories for ordinals like 1st, 2nd, 3rd.
const pr = new Intl.PluralRules("en-US", { type: "ordinal" });
const suffix = { one: "st", two: "nd", few: "rd", other: "th" };
function ordinal(n) { return n + suffix[pr.select(n)]; }
console.log(ordinal(1), ordinal(2), ordinal(3), ordinal(11)); // 1st 2nd 3rd 11thCombining with Numbers
Pair PluralRules with NumberFormat for fully localized count messages.
const nf = new Intl.NumberFormat("en-US");
const pr = new Intl.PluralRules("en-US");
const forms = { one: "file", other: "files" };
function msg(n) { return nf.format(n) + " " + forms[pr.select(n)]; }
console.log(msg(1000)); // "1,000 files"Intl.ListFormat
A bonus: Intl.ListFormat joins items with locale-correct conjunctions.
const lf = new Intl.ListFormat("en-US", { type: "conjunction" });
console.log(lf.format(["red", "green", "blue"])); // "red, green, and blue"Quick Check
Test relative time and plurals.
Recap: Relative Time and Plurals
You formatted relative time with RelativeTimeFormat (including numeric: "auto"), selected unit thresholds, pluralized correctly with PluralRules across languages and ordinals, and joined lists with ListFormat. Next: locale-aware sorting.
Frequently asked questions
Is the “Relative Time and Plurals” lesson free?
Yes — the full text of “Relative Time and Plurals” is free to read here on the web, and the JavaScript Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the JavaScript Academy course, upgrade to CoddyKit PRO.
What will I learn in “Relative Time and Plurals”?
Use RelativeTimeFormat and PluralRules. You practise JavaScript Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start JavaScript Academy?
No prior experience is required. JavaScript Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Relative Time and Plurals” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this JavaScript Academy lesson?
Yes. Every JavaScript Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Formatting Numbers and Currency
- Formatting Dates and Times
- Relative Time and Plurals
- Locale-Aware Sorting