toLocaleString
is a built-in JavaScript method used to convert the date and time to a string using the system locales. 🤓🚀
It can be used with the following JavaScript types 💪:
With dates/time objects, toLocaleString
has a syntax like this and returns a string 🔥👉:
locales
: An optional string that specifies a language-specific format. Some valid values are ar-SA (for Arabic), en-US (for US English), hi-IN (for Hindi), jp-JP (for Japanese), etc.options
: An optional object of options. Some valid properties that can be included in this are dateStyle
with values of full
, long
, medium
and short
. Other possible properties are timeStyle
, weekday
, year
, month
, day
, hour
, minute
, second
, etc.With numbers, toLocaleString
is used to convert the numbers into a locale-specific number representation. It has syntax something like the following and returns a string 🔥👉:
locales
: An optional string the specifies the locale.options
: An optional object that can contain properties such as localeMatcher
with values lookup
and best fit
. Other valied properties are style
, currency
, useGrouping
, minimumSignificantDigits
, etc.With arrays, toLocaleString
is used to convert them into a locale-specific representation. The syntax is as follows and once again a string is returned 🔥👉:
locales
: An optional string specifying the locale.options
: An optional object of the same options available to numbers and dates.Note: If locale is omitted or left undefined than the default system locale is used. 🧪
🥓 Now what’s left is to make sure your targeted browsers support the toLocaleString method.
Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.
While we believe that this content benefits our community, we have not yet thoroughly reviewed it. If you have any suggestions for improvements, please let us know by clicking the “report an issue“ button at the bottom of the tutorial.
This textbox defaults to using Markdown to format your answer.
You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!