Last updated: Mar 4, 2024
Reading timeยท5 min
To format a date as yyyy-mm-dd hh:mm:ss:
10
.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')));
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
).
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'
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:
2
in our case)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.
getMonth
method returns a zero-based month index from 0 to 11 where January is 0
and December is 11
.// ๐๏ธ 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.
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
).
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.
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
.
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(':') ); } // ๐๏ธ 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')));
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
).
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:
2
in our case)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.
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.
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
).
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.
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 function takes a Date
object as a parameter and formats the date as
MM/DD/YYYY hh:mm:ss
.
You can learn more about the related topics by checking out the following tutorials: