Skip to content
GreenSnippets

Format relative time ("3 hours ago") with Intl

Turn a date into a localized relative string using Intl.RelativeTimeFormat, with no library.

JavaScript
const UNITS = [
  ["year", 365 * 24 * 3600],
  ["month", 30 * 24 * 3600],
  ["week", 7 * 24 * 3600],
  ["day", 24 * 3600],
  ["hour", 3600],
  ["minute", 60],
];

function timeAgo(date, { locale = "en", now = Date.now() } = {}) {
  const rtf = new Intl.RelativeTimeFormat(locale, { numeric: "auto" });
  const seconds = Math.round((new Date(date).getTime() - now) / 1000);
  for (const [unit, size] of UNITS) {
    if (Math.abs(seconds) >= size) return rtf.format(Math.trunc(seconds / size), unit);
  }
  return rtf.format(seconds, "second");
}

Example / expected output

Output
timeAgo(Date.now() - 3 * 3600 * 1000) // "3 hours ago"

How it works

Intl.RelativeTimeFormat handles pluralization and localization. The helper picks the largest unit that fits. For server-rendered pages pass a fixed now so server and client output match and you avoid hydration mismatches.