Relative timestamps with Intl

JavaScript has a built-in formatter for relative timestamps. It returns strings like “yesterday” or “2 months from now” in the language you specify.

This does not currently work in IE and Safari.

// a positive value describes a time in the future
new Intl.RelativeTimeFormat('en').format(2, 'day')
// ⇒ 'in 2 days'

// a negative value describes a time in the past
new Intl.RelativeTimeFormat('en').format(-1, 'day')
// ⇒ '1 day ago'

// `numeric: 'auto'` results in strings like “yesterday” and “tomorrow”
new Intl.RelativeTimeFormat('en', { numeric: 'auto' }).format(-1, 'day')
// ⇒ 'yesterday'

// the formatter can base its output on different languages
new Intl.RelativeTimeFormat('de').format(3, 'month')
// ⇒ 'in 3 Monaten'

There is more to learn

Get free previews of my upcoming course materials and other bonus content to help you work smarter. I share tips straight to your inbox once a week. You can read previous mails in the newsletter archive.

Cannot show form because cookie consent was not given.