Format a Date as YYYY-MM-DD hh:mm:ss in JavaScript

avatar

Borislav Hadzhiev

Last updated: Jul 25, 2022

banner

Photo from Unsplash

Format a Date as YYYY-MM-DD hh:mm:ss #

To format a date as yyyy-mm-dd hh:mm:ss:

  1. Get all date components using the methods on the Date object.
  2. Add a leading zero to the day, month, hours, minutes and seconds if the value is less than 10.
  3. Join the date-related strings with a hyphen and the time-related ones with a colon.
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('-') + ' ' + [ padTo2Digits(date.getHours()), padTo2Digits(date.getMinutes()), padTo2Digits(date.getSeconds()), ].join(':') ); } // 👇️ 2021-10-24 16:21:23 (yyyy-mm-dd hh:mm:ss) console.log(formatDate(new Date())); // 👇️️ 2025-05-04 05:24:07 (yyyy-mm-dd hh:mm:ss) console.log(formatDate(new Date('May 04, 2025 05:24:07')));

We first created a padTo2Digits function, which takes care of adding a leading zero if the month, day, hours, minutes or seconds only contain a single digit (are less than 10).

index.js
function padTo2Digits(num) { return num.toString().padStart(2, '0'); } console.log(padTo2Digits(2)); // 👉️ '02' console.log(padTo2Digits(6)); // 👉️ '06' console.log(padTo2Digits(10)); // 👉️ '10'

We want to make sure that the result is always consistent and has 2 digits for the months, days, hours, minutes and seconds, so we used the padStart method.

The first argument we passed to the padTo2Digits function is the total length of the string, so it will never pad a a value if it already has 2 digits.

Next, we created a function that takes a date and formats it to yyyy-mm-dd hh:mm:ss.

The function makes use of the following 6 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.

  • Date.getHours - returns the hour for the specified date.

  • Date.getMinutes - returns the minutes for a date.

  • Date.getSeconds - returns the seconds of 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.

We placed the year, month and day in an array, so we can join them with a hyphen separator.

index.js
console.log(['2024', '06', '22'].join('-')); // 👉️ '2024-06-22' console.log(['2026', '09', '16'].join('-')); // 👉️ '2026-09-16'

This gets us the date formatted as yyyy-mm-dd.

The next step is to place the return values of the time-related methods in an array and join them with a colon separator.

index.js
console.log(['05', '24', '36'].join(':')); // 👉️ '05:24:36' console.log(['08', '13', '56'].join(':')); // 👉️ '08:13:56'

We used the addition (+) operator to add a space in the middle of the strings to get the date formatted as yyyy-mm-dd hh:mm:ss.

Here's the complete code snippet.

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('-') + ' ' + [ padTo2Digits(date.getHours()), padTo2Digits(date.getMinutes()), padTo2Digits(date.getSeconds()), ].join(':') ); } // 👇️ 2021-10-24 16:21:23 (yyyy-mm-dd hh:mm:ss) console.log(formatDate(new Date())); // 👇️️ 2025-05-04 05:24:07 (yyyy-mm-dd hh:mm:ss) console.log(formatDate(new Date('May 04, 2025 05:24:07')));

Further Reading #

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.