Using toLocaleString with Numbers, Arrays or Dates in JavaScript

Published on November 11, 2019
Using toLocaleString with Numbers, Arrays or Dates in JavaScript

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.

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 💪:

  • Dates/Time
  • Numbers
  • Objects
  • Arrays

toLocaleString with Dates and Time 🚀

With dates/time objects, toLocaleString has a syntax like this and returns a string 🔥👉:

dateObject.toLocaleString(locales, options)
  • 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.

Example 😍

const date = new Date();

// 11/10/2019, 4:32:44 PM

// 10/11/2019, 4:32:44 pm

// 10.11.2019 à 16:32:44

const options = {
  weekday: 'long',
  era: 'long'

console.log(date.toLocaleString(`en-US`, options)); 
// Sunday Anno Domini

console.log(date.toLocaleString(`hi-IN`, options));
// ईसवी सन रविवार

console.log(date.toLocaleString(`fr-CH`, options));
// après Jésus-Christ dimanche

toLocaleString with Numbers 🚀

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 🔥👉:

number.toLocaleString(locales, options)
  • 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.

Example 😍

const number = 12345.678;

// 12,345.678

// 12 345,678

console.log(number.toLocaleString('en-US', {
  style: 'currency',
  currency: 'USD'   // With currency, the currency code is also required
}));  // $12,345.68

console.log(number.toLocaleString('hi-IN', {
  style: 'currency',
  currency: 'INR'
}));  // ₹12,345.68

console.log(number.toLocaleString('en-US', {
  style: 'currency',
  currency: 'USD',
  maximumSignificantDigits: 2
}));  // $12,000

toLocaleString with Arrays 🚀

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 🔥👉:

array.toLocaleString(locales, options)
  • locales: An optional string specifying the locale.
  • options: An optional object of the same options available to numbers and dates.

Example 😍

const arr = [12345678, new Date(), "alligators"];
  style: 'currency',
  currency: 'EUR',
  era: 'long'

//  12 345 678,00 €,10 11 2019 après Jésus-Christ à 18:30:03,alligators

const arr2 = [12345678, new Date(), "alligators"];
  style: 'currency',
  currency: 'USD',
  era: 'long'

//  $12,345,678.00,11 10, 2019 Anno Domini, 6:31:56 PM,alligators

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.

Learn more about us

About the authors

Still looking for an answer?

Ask a questionSearch for more help

Was this helpful?
Leave a comment

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!

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Join the Tech Talk
Success! Thank you! Please check your email for further details.

Please complete your information!

Featured on Community

Get our biweekly newsletter

Sign up for Infrastructure as a Newsletter.

Hollie's Hub for Good

Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.

Become a contributor

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

Welcome to the developer cloud

DigitalOcean makes it simple to launch in the cloud and scale up as you grow — whether you're running one virtual machine or ten thousand.

Learn more
DigitalOcean Cloud Control Panel