Skip to main content

How to Get a Date Without the Time in JavaScript

When working with Date objects in JavaScript, a common requirement is to remove the time portion, effectively "zeroing out" the hours, minutes, seconds, and milliseconds to get a Date object that represents the very beginning of the day (midnight).

This guide will teach you the standard, immutable method for setting a date's time to 00:00:00.000. We will also cover how to format a date as a YYYY-MM-DD string, which is a related but distinct task.

The Core Task: Getting a Date Object at Midnight

The goal is to take a Date object that has a time component and create a new Date object for the same day, but with the time set to 00:00:00.000 in the local time zone.

Example:

// Problem: How to get a new Date object for '2025-10-18T00:00:00' from this?
const myDate = new Date('2025-10-18T14:30:15');

The Date.prototype.setHours() method is the most direct and readable tool for this job. It allows you to set the hours, minutes, seconds, and milliseconds all in one call.

This function takes a Date object and returns a new one with the time zeroed out.

function getDateWithoutTime(date) {
// Create a new Date object from the provided date to avoid mutation
const dateCopy = new Date(date);

// Set the time to midnight (00:00:00.000)
dateCopy.setHours(0, 0, 0, 0);

return dateCopy;
}

// Example Usage:
const myDate = new Date('2025-10-18T14:30:15');
const dateAtMidnight = getDateWithoutTime(myDate);

console.log(myDate.toString()); // Output: "Sat Oct 18 2025 14:30:15 GMT..."
console.log(dateAtMidnight.toString()); // Output: "Sat Oct 18 2025 00:00:00 GMT..."
note

This is the recommended best practice because its intent is perfectly clear.

A Note on Immutability

It is a strong best practice to write immutable functions for date manipulation. This means your function should not change the original Date object. Our recommended solution does this by first creating a dateCopy.

The Mutable Approach is not recommended!

// ⛔️ This is not recommended as it mutates the original date.
function zeroOutTime(date) {
date.setHours(0, 0, 0, 0);
}
warning

Mutating the original object can lead to unexpected bugs elsewhere in your application. Always prefer the immutable approach of creating a copy first.

Sometimes, when you say you want a "date without time," what you really need is a string representation of the date portion, like 2025-10-18.

The Intl API is the best tool for this, as it can be configured to produce the desired format. The sv-SE (Swedish) locale happens to use the YYYY-MM-DD format.

const myDate = new Date('2025-10-18T14:30:15');

// The 'sv-SE' locale formats the date as YYYY-MM-DD.
const dateString = myDate.toLocaleDateString('sv-SE');

console.log(dateString); // Output: "2025-10-18"

The Manual Method

You can also build this string yourself.

function formatDate(date) {
const year = date.getFullYear();
// getMonth() is 0-based, so add 1 and pad with a zero.
const month = (date.getMonth() + 1).toString().padStart(2, '0');
const day = date.getDate().toString().padStart(2, '0');

return `${year}-${month}-${day}`;
}

console.log(formatDate(new Date())); // Output: "2025-10-18"

Conclusion

"Getting a date without the time" can mean two different things, and it's important to choose the right tool for your goal.

  • To get a new Date object set to the beginning of the day (midnight), the recommended best practice is to use date.setHours(0, 0, 0, 0) on a copy of your original date.
  • To get a string representation of the date portion (e.g., YYYY-MM-DD), the best modern method is to use date.toLocaleDateString() with the appropriate locale and options.