How to get a Date without the Time in JavaScript

avatar
Borislav Hadzhiev

Last updated: Jan 13, 2023
5 min

banner

# Table of Contents

  1. Get a Date without the Time using JavaScript
  2. Remove the Seconds and Milliseconds from a Date in JS
  3. Remove the Seconds and Milliseconds from a Date without mutation
  4. Formatting a date without the time components

# 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 method 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('-'); } // ๐Ÿ‘‡๏ธ 2023-07-24 (yyyy-mm-dd) console.log(formatDate(new Date())); // ๐Ÿ‘‡๏ธ๏ธ 2025-05-09 (yyyy-mm-dd) console.log(formatDate(new Date(2025, 4, 9)));

get date without time

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

index.js
const date = new Date(); // โœ… Reset a Date's time to midnight date.setHours(0, 0, 0, 0);

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

The setHours method takes values for the hours, minutes, seconds and milliseconds as parameters.

We set all values to 0 to get a date without the time.

# Remove the Seconds and Milliseconds from a Date in JS

Use the setSeconds() method to remove the seconds and milliseconds from a date, e.g. date.setSeconds(0, 0).

The setSeconds method takes the seconds and milliseconds as parameters and sets the provided values on the date.

index.js
const dateStr = '2022-07-21T09:35:31.820Z'; const date = new Date(dateStr); date.setSeconds(0, 0); console.log(date); // ๐Ÿ‘‰๏ธ Thu Jul 21 2022 12:35:00 GMT+0300

remove seconds and milliseconds from date

The two parameters we passed to the Date.setSeconds method are:

  1. seconds - an integer between 0 and 59 that represents the seconds.
  2. milliseconds - a number between 0 and 999 that represents the milliseconds.

The method changes the values of the seconds and milliseconds on the Date object in place.

You can log the ISO representation of the date to see the result.

index.js
const dateStr = '2022-07-21T09:35:31.820Z'; const date = new Date(dateStr); date.setSeconds(0, 0); console.log(date); // ๐Ÿ‘‰๏ธ Thu Jul 21 2022 12:35:00 GMT+0300 // โœ… Seconds and milliseconds removed from a date console.log(date.toISOString()); // ๐Ÿ‘‰๏ธ "2022-07-21T09:35:00.000Z"

You can see the difference between the value of the dateStr variable and the value we logged on the last line.

If you need to remove the time or time zone from a date, check out the following article.

# Remove the Seconds and Milliseconds from a Date without mutation

If you don't want to mutate the Date object in place, you can create a copy of it before using the setSeconds() method.

index.js
const dateStr = '2022-07-21T09:35:31.820Z'; const date = new Date(dateStr); // โœ… Create copy of the Date const copyDate = new Date(date.getTime()); copyDate.setSeconds(0, 0); console.log(copyDate); // ๐Ÿ‘‰๏ธ Thu Jul 21 2022 12:35:00 GMT+0300

remove seconds and milliseconds from date without mutation

The set* methods like setHours, setMinutes and setSeconds mutate the Date object in place, which might not always be what you want.

The getTime method returns the number of milliseconds elapsed between the 1st of January, 1970 and the given date.

We can use this timestamp to create a new Date object with the same date and time, which we can mutate in place.

# Formatting a date without the time components

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.

If you have a Date object and want to set only the Date ignoring the time, you can use the toDateString() method and pass the date string to the Date() constructor.

index.js
const dateAndTime = new Date(2022, 3, 24, 9, 30); const date = new Date(dateAndTime.toDateString()); // ๐Ÿ‘‡๏ธ Sun Apr 24 2022 00:00:00 console.log(date);

The toDateString method returns the date portion of a Date object.

index.js
const dateAndTime = new Date(2022, 3, 24, 9, 30); // ๐Ÿ‘‡๏ธ Sun Apr 24 2022 console.log(dateAndTime.toDateString());

The string returned from the toDateString method does not contain any information about the time, so the date's time gets set to 00:00:00.

# 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