CodePark/ questions / The Intl APIs
START PRACTICINGPRACTICE
Frontend question · Internationalization › The Intl APIs

What does this print?

const rtf = new Intl.RelativeTimeFormat("en", { numeric: "auto" });
console.log(rtf.format(-1, "day"));
console.log(rtf.format(2, "week"));
yesterday and in 2 weeks

With numeric: "auto", the formatter uses words like "yesterday", "today" and "tomorrow" when the language has them. Otherwise it writes "in 2 weeks".

"
1 day ago" is the tempting wrong answer. That is the output with the default, numeric: "always". Note that RelativeTimeFormat only formats. You still calculate the difference and choose the unit yourself.