Vanilla JavaScript date toLocaleString

I'm a full-stack developer from South Africa 🇿🇦. I love writing about JavaScript, HTML and CSS.
Search for a command to run...

I'm a full-stack developer from South Africa 🇿🇦. I love writing about JavaScript, HTML and CSS.
No comments yet. Be the first to comment.
Most of you know me for my consistency, a golden arrow in my blog series. I've written 1000 articles in 1008 days! Almost an article a day, and my honeymoon was the only holiday I ever took. I'm super proud of this achievement; it has been a fantasti...

It's not the first time I'll be talking about community. I think it's an essential aspect of any successful tool. This shows in my previous explorations of Astro, Medusa, and now Vendure as well. All these products thrive in a super open, welcoming, ...

The cool part about Vendure is how easy it is to set up and how abstract each layer is. Basically, we get the following elements: External database Server Worker Admin UI Frontend While this is amazing, it also brings a bit of complexity when it co...

The previous article looked at customizing Vendure on a data and process level. In this article, we'll look at customizing emails, as they are often a big part of a webshop system. We'll be looking at two different layers of customization for customi...

Even though Vendure is a pretty significant project out of the box, in some cases, we might want to go in and modify some elements to work to our specific use case. In this article, I'll take a high-level look at some elements we can customize within...

Before we checked out how to convert numbers to locale formats using JavaScript, and today we'll use the same approach but on date objects.
It's quite often you want to show a date in that user's specific format.
The outputs of today will vary on the locale we pass into the function.
To use this function, we will first need a date object.
const date = new Date('01-10-2020');
This will give us a date format for the 1st of October 2020.
Depending on which locale your country uses, it might look different.
To use this function we must call it upon our date object like so:
console.log(date.toLocaleDateString('en-US'));
That will give us the US annotation and return:
//'1/10/2020'
We can even specify some options on how we would like to receive the output.
const options = {weekday: 'long', year: 'numeric', month: 'long', day: 'numeric'};
console.log(date.toLocaleDateString('de-DE', options));
This will return:
//'Freitag, 10. Januar 2020'
You might have already spotted it above, but we can format it to different locales by setting the locale on the function.
console.log(date.toLocaleDateString('en-US'));
// '1/10/2020'
console.log(date.toLocaleDateString('en-GB'));
// '10/01/2020'
console.log(date.toLocaleDateString('ko-KR'));
// '2020. 1. 10.'
console.log(date.toLocaleDateString('ar-EG'));
// '١٠/١/٢٠٢٠'
console.log(date.toLocaleDateString('nl-NL'));
// '10-1-2020'
Pretty cool, right? If you are wondering where to find these locales, check out this locale list on Stackoverflow.
I've made this Codepen for you guys to play around with and see what happens when you change options or the locales.
Thank you for reading my blog. Feel free to subscribe to my email newsletter and connect on Facebook or Twitter