Format "3 days ago" with Intl.RelativeTimeFormat
Use Intl.RelativeTimeFormat to write labels such as "3 days ago" and "yesterday" in the reader’s locale.
You have a list of posts and each one says "3 days ago". You built the string by hand, with a plural check, and it works in English alone. The French page says "3 days ago" too.
Intl.RelativeTimeFormat builds that string for a supported locale. You give it a locale once, then call format with a number and a unit.
const en = new Intl.RelativeTimeFormat('en')
const de = new Intl.RelativeTimeFormat('de')
const ja = new Intl.RelativeTimeFormat('ja')
console.log(en.format(-3, 'day')) // 3 days ago
console.log(de.format(-3, 'day')) // vor 3 Tagen
console.log(ja.format(-3, 'day')) // 3 日前One call, three languages. A negative number is the past and a positive number is the future, and both 'day' and 'days' are accepted units. The plural, the word order and the word for "ago" come from the locale.
Say "yesterday" when it is yesterday
By default the formatter always uses the number, so en.format(-1, 'day') gives "1 day ago". Pass numeric: 'auto' and it uses a word when the language has one for that number.
const auto = new Intl.RelativeTimeFormat('en', { numeric: 'auto' })
console.log(auto.format(-1, 'day')) // yesterday
console.log(auto.format(0, 'day')) // today
console.log(auto.format(1, 'day')) // tomorrow
console.log(auto.format(-3, 'day')) // 3 days agoThe first three lines print words. The fourth uses a number because English locale data has no special label for three days ago. The same option gives "gestern" in German and "ayer" in Spanish.
From a date to a number
format takes a number, so you do the subtraction. I round the difference in days and pass the result in, because format prints what it is given: -1.5 comes out as "1.5 days ago".
const DAY = 24 * 60 * 60 * 1000
function daysAgo(date, locale, now = Date.now()) {
const days = Math.round((date - now) / DAY)
return new Intl.RelativeTimeFormat(locale, { numeric: 'auto' }).format(days, 'day')
}With now set to 12:00 UTC on 19 June 2025 and a post from 16 June, daysAgo(posted, 'en', now) returns "3 days ago". The same call with 'fr' returns "il y a 3 jours". A post from 18 June gives "yesterday" in English and "ayer" in Spanish.
The same format call takes other units, such as 'hour' and 'month'. With numeric: 'auto', format(-5, 'hour') returns "5 hours ago" and format(-1, 'month') returns "last month", a phrase the default formatter never prints. This helper rounds elapsed 24-hour periods. If your UI should say “yesterday” whenever the date is on the previous calendar day, compare calendar dates in the reader’s time zone first.