Format a Date as YYYY-MM-DD using JavaScript

avatar
Borislav Hadzhiev

Last updated: Mar 6, 2024
6 min

banner

# Table of Contents

  1. Format a Date as YYYY-MM-DD in JavaScript
  2. Format a Date as YYYYMMDD using JavaScript

# Format a Date as YYYY-MM-DD in JavaScript

To format a date as YYYY-MM-DD:

  1. Use the toLocaleString method to get the year formatted as four digits and the month and day formatted as 2 digits.
  2. Use the Array.join() method to join the values with a hyphen separator.
index.js
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)));

format date as yyyy mm dd

The code for this article is available on GitHub

The toLocaleString() method returns a string with language-sensitive representation of the date.

The method takes a locale and an options object and customizes the string according to the supplied values.

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.

The only argument the 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.

# Format a Date as YYYY-MM-DD using getFullYear, getMonth and getDate

To format a date as YYYY-MM-DD:

  1. Use the getFullYear(), getMonth() and getDate() methods to get the year, month and day of the date.
  2. Add a leading zero to the day and month digits if the value is less than 10.
  3. Add the results to an array and join them with a hyphen separator.
index.js
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)));

format date as yyyy mm dd using getfullyear getmonth getdate

The code for this article is available on GitHub

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).

index.js
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.

The first parameter we passed to the 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.

The 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.

index.js
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.

index.js
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.

# Format a Date as YYYYMMDD using JavaScript

To format a date as YYYYMMDD:

  1. Use the toLocaleString method to get the year formatted as four digits and the month and day formatted as 2 digits.
  2. Use the Array.join() method to join the values without a separator.
index.js
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)));

format date as yyyymmdd

The code for this article is available on GitHub

The toLocaleString() method returns a string with language-sensitive representation of the date.

The method takes a locale and an options object and customizes the string according to the supplied values.

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.

If the 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.

# Format a Date as YYYYMMDD using getFullYear, getMonth and getDate

To format a date as YYYYMMDD:

  1. Use the getFullYear(), getMonth() and getDate() methods to get the year, month and day of the date.
  2. Add a leading zero to the day and month digits if the value is less than 10.
  3. Add the results to an array and join them without a separator.
index.js
// โœ… 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)));
The code for this article is available on GitHub

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.

Passing a locale of sv to the toLocaleDateString method returns a Date string formatted as YYYY-MM-DD.
index.js
// ๐Ÿ‘‡๏ธ "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.

index.js
function padTo2Digits(num) { return num.toString().padStart(2, '0'); }
The first parameter we passed to the 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.

The 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.

index.js
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.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.

Copyright ยฉ 2024 Borislav Hadzhiev