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

avatar
Borislav Hadzhiev

Last updated: Mar 4, 2024
5 min

banner

# Table of Contents

  1. Format a Date as YYYY-MM-DD hh:mm:ss in JavaScript
  2. Format a Date as MM/DD/YYYY hh:mm:ss in JavaScript

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

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(':') ); } // ๐Ÿ‘‡๏ธ 2023-01-04 10:00:07 console.log(formatDate(new Date())); // ๐Ÿ‘‡๏ธ๏ธ 2025-05-04 05:24:07 console.log(formatDate(new Date('May 04, 2025 05:24:07')));

format date as yyyy mm dd hh mm ss

The code for this article is available on GitHub

The padTo2Digits function takes care of adding a leading zero if the month, day, hours, minutes and 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' console.log(padTo2Digits(27)); // ๐Ÿ‘‰๏ธ '27'

using pad to 2 digits function

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 String.padStart() method.

We passed the following 2 arguments to the padStart() method:

  1. the total length of the string (2 in our case)
  2. the pad character (0 in our case)

The padStart method will never pad the values to more than 2 characters because we set the target length to 2.

We then 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 methods on the Date object:

  • Date.getFullYear() - returns a four-digit number representing the year of the given date.

  • Date.getMonth() - returns an integer between 0 (January) and 11 (December) and represents the month of 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 of the specified date.

  • Date.getMinutes() - returns the minutes for the specified date.

  • Date.getSeconds() - returns the seconds for the specified date.

The getMonth method returns a zero-based month index from 0 to 11 where January is 0 and December is 11.
index.js
// ๐Ÿ‘‡๏ธ January 17th 2025 console.log(new Date(2025, 0, 17)); // ๐Ÿ‘‡๏ธ December 17th 2025 console.log(new Date(2025, 11, 17));

We had to add 1 to the output of the getMonth method to get the expected result.

We placed the year, month and day in an array, so we could 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 (hh:mm:ss).

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 and time formatted as YYYY-MM-DD hh:mm:ss.

It is best to define a reusable function to not have to remember that you have to add 1 to the output of the getMonth() method.

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')));

The function takes a Date object as a parameter and formats the date as YYYY-MM-DD hh:mm:ss.

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

To format a date as MM/DD/YYYY hh:mm:ss:

  1. Get all the components of the date 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 forward slash and the time-related ones with a colon.
index.js
function padTo2Digits(num) { return num.toString().padStart(2, '0'); } function formatDate(date) { return ( [ padTo2Digits(date.getMonth() + 1), padTo2Digits(date.getDate()), date.getFullYear(), ].join('/') + ' ' + [ padTo2Digits(date.getHours()), padTo2Digits(date.getMinutes()), padTo2Digits(date.getSeconds()), ].join(':') ); } // ๐Ÿ‘‡๏ธ 07/24/2023 16:11:58 (MM/DD/YYYY hh:mm:ss) console.log(formatDate(new Date())); // ๐Ÿ‘‡๏ธ๏ธ 05/04/2025 05:24:07 (MM/DD/YYYY hh:mm:ss) console.log(formatDate(new Date('May 04, 2025 05:24:07')));

format date as mm dd yyyy hh mm ss

The code for this article is available on GitHub

The padTo2Digits function 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(1)); // ๐Ÿ‘‰๏ธ '01' console.log(padTo2Digits(5)); // ๐Ÿ‘‰๏ธ '05' 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 String.padStart() method.

The arguments we passed to the padStart() method are:

  1. the target length of the string (2 in our case)
  2. the pad character (0 in our case)

We set the target length argument to 2, so the padStart method won't pad the date and time components if they already consist of 2 digits.

We then created a function that takes a date and formats it as MM/DD/YYYY hh:mm:ss.

The function makes use of the following 6 methods on the Date object:

  • Date.getMonth() - returns an integer between 0 (January) and 11 (December) and represents the month of 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 the specified date.

  • Date.getFullYear() - returns a four-digit number representing the year that corresponds to the specified date.

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

  • Date.getMinutes() - returns the minutes for the specified date.

  • Date.getSeconds() - returns the seconds for the specified date.

The getMonth method returns a zero-based month index from 0 to 11 where January is 0 and December is 11.

The getMonth method is zero-based, so had to add 1 to its return value to get the expected result.

We placed the return values of the date-related methods in an array, so we can join them with a forward slash / separator.

index.js
console.log(['06', '26', '2026'].join('/')); // ๐Ÿ‘‰๏ธ '06/26/2026' console.log(['11', '16', '2025'].join('/')); // ๐Ÿ‘‰๏ธ '11/16/2025'

This gets us a date formatted as MM/DD/YYYY.

We added the return values of the time-related methods into an array and joined them with a colon separator (hh:mm:ss).

index.js
console.log(['09', '12', '36'].join(':')); // ๐Ÿ‘‰๏ธ '09:12:36' console.log(['06', '36', '56'].join(':')); // ๐Ÿ‘‰๏ธ '06:36:56'

We used the addition (+) operator to add a space between the date and time related strings to get a date formatted as MM/DD/YYYY hh:mm:ss.

It's best to create a reusable function to not have to remember that the getMonth() method returns a zero-based value.

index.js
function padTo2Digits(num) { return num.toString().padStart(2, '0'); } function formatDate(date) { return ( [ padTo2Digits(date.getMonth() + 1), padTo2Digits(date.getDate()), date.getFullYear(), ].join('/') + ' ' + [ padTo2Digits(date.getHours()), padTo2Digits(date.getMinutes()), padTo2Digits(date.getSeconds()), ].join(':') ); } // ๐Ÿ‘‡๏ธ 10/24/2021 16:51:53 (mm/dd/yyyy hh:mm:ss) console.log(formatDate(new Date())); // ๐Ÿ‘‡๏ธ๏ธ 05-04-2025 05:24:07 (mm/dd/yyyy hh:mm:ss) console.log(formatDate(new Date('May 04, 2025 05:24:07')));
The code for this article is available on GitHub

The function takes a Date object as a parameter and formats the date as MM/DD/YYYY hh:mm:ss.

# 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