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'
More fire tips
Checking if something is an array
We cannot use the typeof operator in JavaScript to check if something is an array or not. Luckily, there is a helper function on the Array prototype for that.
Read fire tip