DEV Community

Cover image for .toLocaleString, one of the most underrated JavaScript features

Posted on • Originally published at

.toLocaleString, one of the most underrated JavaScript features

.toLocaleString and friends are some of the most underrated features of JavaScript. I came over them through a few different wanderings through MDN and I've used them in like every project since.

Here, I'll show you how you can use them in your own code.

.toLocaleString is for formatting

.toLocaleString is a method present on dates and numbers, which is used to format them in a locale-specific way.

new Date().toLocaleString()
// => 24/4/2022, 10:40:00 am
Enter fullscreen mode Exit fullscreen mode

By default, it will use the browser's locale, but you can specify a different one with the locale parameter.

console.log(new Date().toLocaleString('en-US'))
// => 4/24/2022, 10:40:00 AM
console.log(new Date().toLocaleString('en-GB'))
// => 24/04/2022, 10:40:00
console.log(new Date().toLocaleString('ko-KR'))
// => 2022. 4. 24. 오전 10:40:49
Enter fullscreen mode Exit fullscreen mode

You can further customize the output by specifying the format of the date.

console.log(new Date().toLocaleString('en-US', {
    year: 'numeric',
    weekday: 'long',
    month: 'long',
    day: 'numeric',
    hour: 'numeric',
    minute: 'numeric',
    second: 'numeric',
    hour12: false,
// => Sunday, April 24, 2022 at 10:40:00

console.log(new Date().toLocaleString('en-US', {
    dateStyle: 'full',
// => Sunday, April 24, 2022

console.log(new Date().toLocaleString('en-US', {
    dateStyle: 'full',
    timeStyle: 'full',
// => Sunday, April 24, 2022 at 10:40:00 AM India Standard Time

console.log(new Date().toLocaleString('en-US', {
    calendar: 'indian',
// => 2/4/1944 Saka, 10:40:00 AM
// I don't know what that means either

console.log(new Date().toLocaleString('en-US', {
    dayPeriod: 'long',
// => in the morning

console.log(new Date().toLocaleString('en-US', {
    era: 'long',
    dayPeriod: 'long',
    weekday: 'long',
    month: 'long',
    year: 'numeric',
    day: '2-digit',
    hour: '2-digit',
    minute: '2-digit',
    second: '2-digit',
    fractionalSecondDigits: 3,
    timeZoneName: 'long',
// => Sunday, April 24, 2022 Anno Domini at 10:00:00.124 in the morning India Standard Time
Enter fullscreen mode Exit fullscreen mode

This totally eliminates the need of date formatting libraries like Moment.js in your code!

Numbers too!

.toLocaleString is also a method present on numbers, which is used to format them in a locale-specific way.

// => 10,000,000
Enter fullscreen mode Exit fullscreen mode

As usual, you can specify a different locale with the locale parameter.

// => ١٠٬٠٠٠٬٠٠٠
// Another language I know
Enter fullscreen mode Exit fullscreen mode

This one also has options.

// currency
10000..toLocaleString('en-US', {style: 'currency', currency: 'USD'})
// => $10,000.00

10000..toLocaleString('en-US', {style: 'currency', currency: 'USD', currencyDisplay: 'name'})
// => 10,000.00 US dollars

(-11.29).toLocaleString('en-US', {style: 'currency', currency: 'USD', currencySign: 'accounting'})
// => ($11.29)

(-11.29).toLocaleString('en-US', {style: 'currency', currency: 'USD', currencySign: 'standard'})
// => -$11.29

// scientific
10000..toLocaleString('en-US', {notation: 'scientific'})
// => 1E4

10000..toLocaleString('en-US', {notation: 'compact'})
// => 10K
1234..toLocaleString('en-US', {notation: 'compact'})
// => 1.2K

1234..toLocaleString('en-US', {notation: 'engineering'})
// => 1.234E3

1234..toLocaleString('en-US', {notation: 'engineering', signDisplay: 'always'})
// => +1.234E3

0.55.toLocaleString('en-US', {style: 'percent'})
// => 55%

1234..toLocaleString('en-US', {style: 'unit', unit: 'liter'})
// => 1,234 L

1234..toLocaleString('en-US', {style: 'unit', unit: 'liter', unitDisplay: 'narrow'})
// => 1,234L
Enter fullscreen mode Exit fullscreen mode

Once again, this removes the need for a ton of libraries for number formatting!

That was one of the most surprising JavaScript moments for me. Sure, I knew that JavaScript was aware of timezones, but getting access to a whole formatting library? 🤯

Top comments (28)

joelbonetr profile image
JoelBonetR 🥇

Not only in JS, there are bugs in some games due to forgetting about this when dealing with dates in C# in Unity and so on 😆

siddharthshyniben profile image

Haha, dates are a pain

birki profile image
Marcel Birkenkamp

Blain is a Pain

Thread Thread
joelbonetr profile image
JoelBonetR 🥇 • Edited

No Pain no Blain 💪🏼😂

eziyadah profile image
Eiyad Z.

I have used it for dates before, but really didn’t realize that I can do all of this ☺️

Great Article! 👏🏻👏🏻

yukimyona profile image
Oluwapelumi Odumosu • Edited

My mind is blown, I've used it a ton of times for date formatting but didn't even know it had some more properties for dates! And that it can also be used for numbers 🤯.
I'm definitely trying this out.
Thanks for this piece.

busyxiang profile image

Didn't even realise you can do all this, thank you for the sharing

venkatesanmca008 profile image

good one ..

ricky11 profile image
Rishi U

awesome, why the .. for some items and not others?

siddharthshyniben profile image
Siddharth • Edited

In JavaScript, you can't directly access properties on numbers. You can either use 2 dots or braces instead

10.toString() // error
(10).toString() // ok
10..toString() // ok
Enter fullscreen mode Exit fullscreen mode
triyanox profile image
Mohamed Achaq

I'm using this feature a lot when displaying dates !

frodolight profile image

Thank you.

thomasbnt profile image
Thomas Bnt

Good post and useful feature 👌

siddharthshyniben profile image


yashdesai profile image
Yash Desai

Thanks for sharing 👍

amircahyadi profile image

Nice 👍

mat3uszkek profile image
Mateusz Skrobiś

I didn't know about that - WOW! this is awesome :) thank u very much for this article!

augustoapg profile image
Augusto Peres

MIND BLOWN! I’ll use it for sure!! Thanks!!

arunagnihotri profile image
Arun Agnihotri

I used it for dates only but very basic only. Didn't know all this was possible.
Thanks Siddharth!!

elainedelgado profile image
Elaine Delgado

Thanks, that helped a lot!!
Goodbye libraries for number formatting! =D