Get a Date without the Time using JavaScript

avatar

Borislav Hadzhiev

Last updated: Jul 25, 2022

banner

Photo from Unsplash

Get a Date without the Time using JavaScript #

Use the setHours() method to get a date without the time, e.g. date.setHours(0, 0, 0, 0). The setHours methods takes the hours, minutes, seconds and milliseconds as parameters and changes the Date object according to the provided values.

index.js
const date = new Date(); // ✅ Reset a Date's time to midnight date.setHours(0, 0, 0, 0); // ✅ Format a date to YYYY-MM-DD (or any other format) function padTo2Digits(num) { return num.toString().padStart(2, '0'); } function formatDate(date) { return [ date.getFullYear(), padTo2Digits(date.getMonth() + 1), padTo2Digits(date.getDate()), ].join('-'); } // 👇️ 2022-01-18 (yyyy-mm-dd) console.log(formatDate(new Date())); // 👇️️ 2025-05-09 (yyyy-mm-dd) console.log(formatDate(new Date(2025, 4, 9)));

The setHours method can be used if you need to reset a date's time to midnight (00:00:00).

The method changes the date's time components in place.

If you need to format a Date object, you can create a helper function like the one above.

The function formats a date as YYYY-MM-DD, but this can be adjusted according to your use case.

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.

The getMonth method is zero-based, so 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(['2022', '01', '18'].join('-')); // 👉️ '2022-01-18' console.log(['2024', '09', '24'].join('-')); // 👉️ '2024-09-24'

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

You could reorder the date components in the array, change the separator to a forward slash instead of a hyphen, or change the function in any other way.

Here is an example that formats the date as MM/DD/YYYY.

index.js
// ✅ Format a date as MM/DD/YYYY (or any other format) function padTo2Digits(num) { return num.toString().padStart(2, '0'); } function formatDate(date) { return [ padTo2Digits(date.getMonth() + 1), date.getFullYear(), padTo2Digits(date.getDate()), ].join('/'); } // 👇️ 01/2022/18 (MM/DD/YYYY) console.log(formatDate(new Date())); // 👇️️ 05/2025/09 (MM/DD/YYYY) console.log(formatDate(new Date(2025, 4, 9)));

All we had to do to change the formatting is to reorder the elements in the array and change the separator to a forward slash.

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.