javascript / intermediate
Snippet
Localized Relative Times
Intl.RelativeTimeFormat enables language-sensitive relative time formatting (e.g., 'yesterday', 'in 3 months'). The 'numeric: auto' option uses idiomatic phrases instead of literal numbers where possible.
snippet.js
1
2
3
4
5
6
const rtf = new Intl.RelativeTimeFormat('de', {numeric: 'auto'});console.log(rtf.format(-1, 'day'));console.log(rtf.format(3, 'month'));console.log(rtf.format(-2, 'second'));
Breakdown
1
new Intl.RelativeTimeFormat('de', ...)
Creates a formatter for a specific locale (in this case, German).
2
numeric: 'auto'
Enables text like 'yesterday' instead of '1 day ago'.
3
rtf.format(-1, 'day')
Calculates the string for a relative point in time based on the unit provided.