Skip to content

Instantly share code, notes, and snippets.

@jkasaudhan
Last active May 26, 2019 12:27
Show Gist options
  • Save jkasaudhan/6bc49513286eda30aedfd40f0fadbd42 to your computer and use it in GitHub Desktop.
Save jkasaudhan/6bc49513286eda30aedfd40f0fadbd42 to your computer and use it in GitHub Desktop.
Browser's internationalization api RelativeTimeFormat
// For english version - it can be applied to second, minute,
// day, week, hour, month and quarter
const rtf = new Intl.RelativeTimeFormat('en');
rtf.format(2, 'day')
// in 2 days
rtf.format(-2, 'day')
// two days ago
rtf.format(-12, 'minute')
// 12 minutes ago
rtf.format(12, 'minute')
// in 12 minutes
// For german version
const rtfde = new Intl.RelativeTimeFormat('de');
rtfde.format(2, 'day')
// in 2 Tagen
rtfde.format(-2, 'day')
// vor 2 Tagen
rtfde.format(-12, 'minute')
// vor 12 Minuten
rtfde.format(12, 'minute')
// in 12 Minuten
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment