Borislav Hadzhiev
Sun Oct 24 2021·3 min read
Photo by Jezael Melgoza
To format a date as MM/DD/YYYY hh:mm:ss:
10
.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 first thing we did is create a padTo2Digits
function, which will take care
of adding a leading zero if the month, day, hours, minutes or seconds only
contain a single digit (are less than 10).
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 padStart method.
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
mm/dd/yyyy hh:mm:ss
.
The function makes use of the following 6 Date
related methods.
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.getFullYear method - returns a four-digit number representing the year that corresponds to a 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.
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.
We placed the return values from the date-related methods in an array, so we can
join them with a forward slash /
separator.
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 from the time-related methods into an array and joined them with a colon separator.
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
.
Here's the complete code snippet.
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')));