How to Format a Date as YYYY-MM-DD in JavaScript

avatar

Borislav Hadzhiev

Sun Oct 24 20212 min read

banner

Photo by Jeremy Bishop

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

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 corresponding date.
  2. Add a leading zero to the day and month digits if the value is less than 10 to always get consistent results.
  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('-'); } // ๐Ÿ‘‡๏ธ 2025-10-24 (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 is create a padTo2Digits function, which will take care of adding a leading zero if the month or day only contain 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 padStart method.

The first parameter we passed to the padTo2Digits function is the total length of the string, so it will never pad a 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.

Because the getMonth method is zero-based 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) { return [ date.getFullYear(), padTo2Digits(date.getMonth() + 1), padTo2Digits(date.getDate()), ].join('-'); } // ๐Ÿ‘‡๏ธ 2025-10-24 (yyyy-mm-dd) console.log(formatDate(new Date())); // ๐Ÿ‘‡๏ธ๏ธ 2025-05-09 (yyyy-mm-dd) console.log(formatDate(new Date(2025, 4, 9)));

Further Reading #

Join my newsletter

I'll send you 1 email a week with links to all of the articles I've written that week

Buy Me A Coffee