How to format JavaScript date and time with Intl.DateFormat
Intl Object in JavaScript
Intl ( internationalization )is an in-built Object in JavaScript for language-sensitive formatting. It includes constructors for formatting strings, lists, numbers, and dates and times. In this post, I am going to talk about JavaScript date and time formatting with Intl.DateTimeFormat() constructor.
Intl.DateTimeFormat()
Intl.DateTimeFormat() constructor creates an instance of Intl.DateTimeFormat
. You can use this to apply formattings with its instance methods.
You can create an Intl.DateTimeFormat object in one of the following ways.
new Intl.DateTimeFormat();
new Intl.DateTimeFormat(locales);
new Intl.DateTimeFormat(locales, options);
//or
Intl.DateTimeFormat();
Intl.DateTimeFormat(locales);
Intl.DateTimeFormat(locales, options);
As you can notice Intl.DateTimeFormat()
takes a maximum of two parameters. These parameters are critical in applying language-sensitive formatting.
So, let’s talk about these parameters.
How to use the locales parameter
- This a string of BCP 47 language subtags
- You can also have an array of strings these strings