0

Is it possible to make a plural form of hour and hours through momentjs? Or maybe something native from Intl.

I looking for something like this, because I need localization as well.

moment.specialFunction({hours: 1}); // 1 hour
moment.specialFunction({hours: 2}); // 2 hours
moment.locale("ru").specialFunction({hours: 1}); // 1 час

Nikita
  • 317
  • 3
  • 10

1 Answers1

1

This can be achieved using Intl.NumberFormat:

console.log(
  Intl.NumberFormat('en-GB', { style: 'unit', unit: 'hour', unitDisplay: 'long' }).format(12),
  Intl.NumberFormat('ru', { style: 'unit', unit: 'hour', unitDisplay: 'long' }).format(12),
  Intl.NumberFormat('en-US', { style: 'unit', unit: 'hour', unitDisplay: 'long' }).format(1),
  Intl.NumberFormat('ru', { style: 'unit', unit: 'hour', unitDisplay: 'long' }).format(1),
)
evolutionxbox
  • 3,932
  • 6
  • 34
  • 51