The Intl object is the namespace for the ECMAScript Internationalization API, which provides language sensitive string comparison, number formatting, and date and time formatting. The Intl object provides access to several constructors as well as functionality common to the internationalization constructors and other language sensitive functions.
Source: https://developer.mozilla.org/
So, in short, it's an object that holds some ready-made functions that help format number and date with various locale options to choose.
There are many things we can do with Intl object.
Below are the list decendending from the most frequently used, in my opinion.
This will display the nuber with decimal separator and some extra display options.
string | string[]
, language code can be found here. You can also put a fall-back locale in case the first one is not supported in an array.style
: to declare the foramt type, either it's percent/currency/decimal or a unit. When use currency option, you need to provide the currency as well, like this new Intl.NumberFormat({ style: 'unit'}).format(number)
When no locales or options were provided, default locale is your browser's locale and the style is decimal.
string | string[]
, language code can be found here. You can also put a fall-back locale in case the first one is not supported in an array.options = { hour: 'numeric', minute: 'numeric', second: 'numeric', timeZone: 'Australia/Sydney', timeZoneName: 'short' };
This return the term "a day ago" ot "in a day".
string | string[]
, language code can be found here. You can also put a fall-back locale in case the first one is not supported in an array.The remaining function are just returning the value in literal form.
"a, b and c"
or "a,b or c"
Check out mozilla site for more details. See you in another post 🤟
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl