How to Set the Time of a Date to Midnight in JavaScript
When working with dates, a common requirement is to "zero out" or "strip" the time portion of a Date object, effectively setting its time to the beginning of the day (midnight, 00:00:00). This is crucial for comparing dates without the time component influencing the result, or for standardizing dates for storage or display.
This guide will demonstrate the modern and most robust method for this task using Date.prototype.setHours(). We will also cover a more traditional approach using the Date constructor for a complete understanding.
The Core Task: Setting a Date to Midnight
The goal is to take a Date object that has a time component (e.g., 2025-10-24T14:30:15.000Z) and create a new Date object representing the very start of that same day.
Problem: you have a Date object and need to ignore its time part for a comparison or calculation.
// Problem: These two dates represent the same day, but are not equal
// because their time components are different.
const date1 = new Date('2025-10-24T10:00:00');
const date2 = new Date('2025-10-24T18:00:00');
console.log(date1.getTime() === date2.getTime()); // Output: false
To compare them just by date, we must first set both of their times to midnight.
The Modern Method (Recommended): setHours(0, 0, 0, 0)
The setHours() method is the cleanest and most direct way to achieve this. It modifies the Date object in place and returns its new timestamp.
Logic: the setHours() method can accept four arguments: hours, minutes, seconds, and milliseconds. By setting all of them to 0, we reset the time to the beginning of the day in the date's local timezone.
Solution: this function takes a Date and returns a new Date object with the time set to midnight, preserving the original.
function setDateToMidnight(date) {
// Create a new Date object from the original to avoid mutation.
const newDate = new Date(date);
// Set hours, minutes, seconds, and milliseconds to 0.
newDate.setHours(0, 0, 0, 0);
return newDate;
}
// Example Usage:
const now = new Date(); // e.g., '2025-10-24T14:30:15.123Z'
const todayAtMidnight = setDateToMidnight(now);
console.log(now);
console.log(todayAtMidnight);
// Output:
// Fri Oct 24 2025 10:30:15 GMT-0400 (Eastern Daylight Time)
// Fri Oct 24 2025 00:00:00 GMT-0400 (Eastern Daylight Time)
The Classic Method: Reconstructing the Date
Another common approach is to create a completely new Date object, providing only the year, month, and day from the original date.
function setDateToMidnight(date) {
return new Date(
date.getFullYear(),
date.getMonth(),
date.getDate()
);
}
// Example Usage:
const now = new Date();
const todayAtMidnight = setDateToMidnight(now);
console.log(todayAtMidnight);
// Output: Fri Oct 24 2025 00:00:00 GMT-0400 (Eastern Daylight Time)
Observe that:
getFullYear(): Returns the 4-digit year.getMonth(): Returns the month, zero-indexed (0 for January, 11 for December).getDate(): Returns the day of the month (1-31).
When the time components are omitted from the Date constructor, they default to 0, which achieves our goal. While this works perfectly, the setHours() method is often considered more explicit about its intent.
A Note on Timezones: Local vs. UTC
It is critical to understand that both methods shown above operate in the local timezone of the environment where the code is running.
setHours()sets the time to midnight in the local timezone.getFullYear(),getMonth(), andgetDate()all return values based on the local timezone.
If you need to set the time to midnight in UTC, you must use the corresponding UTC methods.
The Solution for UTC:
function setDateToMidnightUTC(date) {
const newDate = new Date(date);
// Use the UTC-specific methods.
newDate.setUTCHours(0, 0, 0, 0);
return newDate;
}
const now = new Date('2025-10-24T14:30:15Z'); // A specific time in UTC
const todayAtMidnightUTC = setDateToMidnightUTC(now);
console.log(now.toISOString()); // Output: 2025-10-24T14:30:15.000Z
console.log(todayAtMidnightUTC.toISOString()); // Output: 2025-10-24T00:00:00.000Z
Choosing between local and UTC depends on your application's requirements. For server-side logic or storing dates, UTC is almost always the correct choice.
Conclusion
Setting the time of a Date object to midnight is an essential task for standardizing and comparing dates.
- The
date.setHours(0, 0, 0, 0)method is the recommended best practice. It is explicit, readable, and clearly states its intent. (Remember to create a newDateobject first to avoid mutating the original). - Creating a new
Dateobject withnew Date(date.getFullYear(), date.getMonth(), date.getDate())is a perfectly valid and common alternative. - Always be mindful of timezones. Use the standard
setHours()for local time andsetUTCHours()when you need to operate in UTC.