Format a Date as YYYYMMDD using JavaScript

avatar

Borislav Hadzhiev

Last updated: Jul 25, 2022

banner

Check out my new book

Format a Date as YYYYMMDD using JavaScript #

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); // 👉️ "20220119" (today is Jan 19th, 2022) // ✅ 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(''); } // 👇️ 20220119 (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 snippet 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
// 👇️ "2022-01-19" console.log(new Date().toLocaleDateString('sv'))

All we had to do to get the date formatted as YYYYMMDD is remove the hyphens.

In the second example, we created a reusable function which 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 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. Here is the complete code snippet.

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(''); } // 👇️ 20220119 (get today's date) (yyyymmdd) console.log(formatDate()); // 👇️️ 20250509 (yyyymmdd) console.log(formatDate(new Date(2025, 4, 9)));
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.