Last updated: Mar 6, 2024
Reading timeยท6 min
To format a date as YYYY-MM-DD:
toLocaleString
method to get the year formatted as four digits and
the month and day formatted as 2 digits.Array.join()
method to join the values with a hyphen separator.function formatDate(date = new Date()) { const year = date.toLocaleString('default', {year: 'numeric'}); const month = date.toLocaleString('default', { month: '2-digit', }); const day = date.toLocaleString('default', {day: '2-digit'}); return [year, month, day].join('-'); } // ๐๏ธ 2023-07-26 (YYYY-MM-DD) console.log(formatDate(new Date())); // ๐๏ธ๏ธ 2025-05-09 (YYYY-MM-DD) console.log(formatDate(new Date(2025, 4, 9)));
The toLocaleString() method returns a string with language-sensitive representation of the date.
When the year is set to numeric
, it gets formatted to 4 digits.
We formatted the month and day to 2 digits by using the 2-digit
value.
The last step is to place the date components in an array and join them with a hyphen separator.
The Array.join() method concatenates all of the elements in an array using a separator.
Array.join()
method takes is a separator
- the string used to separate the elements of the array.The function formats the given date as YYYY-MM-DD
and returns the result.
Alternatively, you can use the getMonth
and getDate()
methods.
To format a date as YYYY-MM-DD:
getFullYear()
, getMonth()
and getDate()
methods to get the
year, month and day of the date.10
.function padTo2Digits(num) { return num.toString().padStart(2, '0'); } function formatDate(date) { return [ date.getFullYear(), padTo2Digits(date.getMonth() + 1), padTo2Digits(date.getDate()), ].join('-'); } // ๐๏ธ 2023-07-26 (yyyy-mm-dd) console.log(formatDate(new Date())); // ๐๏ธ๏ธ 2025-05-09 (yyyy-mm-dd) console.log(formatDate(new Date(2025, 4, 9)));
The first thing we did was create a padTo2Digits
function that will take care
of adding a leading zero if the
month or day only contains a single digit (are less than 10).
function padTo2Digits(num) { return num.toString().padStart(2, '0'); } console.log(padTo2Digits(5)); // ๐๏ธ '05' console.log(padTo2Digits(9)); // ๐๏ธ '09' console.log(padTo2Digits(10)); // ๐๏ธ '10'
We want to make sure that the result is always consistent and has 2 digits for the months and days, so we used the String.padStart method.
padTo2Digits
function is the total length of the string, so it will never pad the day or month if they already have 2 digits.Next, we created a function that takes a date and formats it to YYYY-MM-DD
.
The function makes use of the following 3 Date
related methods.
Date.getFullYear() method - returns a four-digit number representing the year that corresponds to a date.
Date.getMonth() - returns an
integer between 0
(January) and 11
(December) and represents the month for
a given date. Yes, unfortunately, the getMonth
method is off by 1
.
Date.getDate() - returns an
integer between 1
and 31
representing the day of the month for a specific
date.
getMonth
method returns a zero-based month index from 0 to 11, meaning January is 0
and December is 11
.The getMonth
method is zero-based, so we added 1 to its return value.
The last step is to place the calls to the methods in an array, so we can join them by a hyphen separator. Here are some examples.
console.log(['2025', '05', '24'].join('-')); // ๐๏ธ '2025-05-24' console.log(['2025', '09', '13'].join('-')); // ๐๏ธ '2025-09-13'
This gets us the date, formatted as YYYY-MM-DD
. Here's the complete example.
function padTo2Digits(num) { return num.toString().padStart(2, '0'); } function formatDate(date = new Date()) { return [ date.getFullYear(), padTo2Digits(date.getMonth() + 1), padTo2Digits(date.getDate()), ].join('-'); } // ๐๏ธ 2023-07-26 (yyyy-mm-dd) console.log(formatDate()); // ๐๏ธ๏ธ 2025-05-09 (yyyy-mm-dd) console.log(formatDate(new Date(2025, 4, 9)));
If you need to format a date as YYYYMMDD
, check out the next subheading.
To format a date as YYYYMMDD:
toLocaleString
method to get the year formatted as four digits and
the month and day formatted as 2 digits.Array.join()
method to join the values without a separator.function formatDate(date = new Date()) { const year = date.toLocaleString('default', {year: 'numeric'}); const month = date.toLocaleString('default', { month: '2-digit', }); const day = date.toLocaleString('default', {day: '2-digit'}); return [year, month, day].join(''); } // ๐๏ธ 20230726 (YYYYMMDD) console.log(formatDate(new Date())); // ๐๏ธ๏ธ 20250509 (YYYYMMDD) console.log(formatDate(new Date(2025, 4, 9)));
The toLocaleString() method returns a string with language-sensitive representation of the date.
When the year is set to numeric
, it gets formatted as 4 digits.
We formatted the month and day to 2 digits by using the 2-digit
value.
The last step is to place the date components in an array and join them without a separator.
The only argument the Array.join()
method takes is a separator
- the string
used to separate the elements of the array.
separator
argument is set to an empty string, the array elements are joined without any characters in between them.The function formats the given date as YYYYMMDD
and returns the result.
To format a date as YYYYMMDD
:
getFullYear()
, getMonth()
and getDate()
methods to get the
year, month and day of the date.10
.// โ Using a one-liner hack const result = new Date() .toLocaleDateString('sv') .replaceAll('-', ''); console.log(result); // ๐๏ธ "20230726" (today is Jan 13th, 2023) // ---------------------------------------------------------- // โ Or create a reusable function function padTo2Digits(num) { return num.toString().padStart(2, '0'); } function formatDate(date = new Date()) { return [ date.getFullYear(), padTo2Digits(date.getMonth() + 1), padTo2Digits(date.getDate()), ].join(''); } // ๐๏ธ 20230726 (get today's date) (YYYYMMDD) console.log(formatDate()); // ๐๏ธ๏ธ 20250509 (YYYYMMDD) console.log(formatDate(new Date(2025, 4, 9)));
We formatted a date as YYYYMMDD
in two different ways.
The first example in the code sample shows a quick, one-liner hack you can use.
sv
to the toLocaleDateString
method returns a Date
string formatted as YYYY-MM-DD
.// ๐๏ธ "2023-01-13" console.log(new Date().toLocaleDateString('sv'))
All we had to do to get the date formatted as YYYYMMDD
was remove the hyphens.
In the second example, we created a reusable function that takes a date as a
parameter and formats it as YYYYMMDD
.
If no date is provided, the function returns the current date in the same format.
We want to make sure that the result is always consistent and has 2 digits for the months and days, so we used the String.padStart method.
function padTo2Digits(num) { return num.toString().padStart(2, '0'); }
padStart
method is the total length of the string, so it will never pad the day or month if they already have 2 digits.The next function we created is the one that formats a date as YYYYMMDD
.
The function makes use of the following 3 Date
related methods:
Date.getFullYear() method - returns a four-digit number representing the year that corresponds to a date.
Date.getMonth() - returns an
integer between 0
(January) and 11
(December) and represents the month for
a given date. Yes, unfortunately, the getMonth
method is off by 1
.
Date.getDate() - returns an
integer between 1
and 31
representing the day of the month for a specific
date.
getMonth
method returns a zero-based month index from 0 to 11, meaning January is 0
and December is 11
.The getMonth
method is zero-based, so we added 1 to its return value.
The last step is to place the calls to the methods in an array, so we can join them without a separator. Here are some examples.
console.log(['2022', '01', '19'].join('')); // ๐๏ธ '20220119' console.log(['2024', '09', '24'].join('')); // ๐๏ธ '20240924'
This gets us the date formatted as YYYYMMDD
.
I've also written an article on how to format a date as YYYY-MM-DD using JavaScript.
You can learn more about the related topics by checking out the following tutorials: