Compare Date Parts Without Comparing Time in JavaScript

Last Updated : 31 Aug, 2026

When comparing two dates in JavaScript, the time portion is also considered by default. To compare only the date part and ignore hours, minutes, seconds, and milliseconds, we can normalize both dates before comparing them.

These are the following approaches to compare dates without considering the time:

Approach 1: Using setHours() Method

In this approach, we set the hours, minutes, seconds, and milliseconds of both date objects to 0 using the setHours() method. This removes the time difference, allowing us to compare only the date values.

Syntax:

date.setHours(0, 0, 0, 0);

Example: Compare two dates after resetting their time values to midnight.

javascript
<script>
    let date1 = new Date();
    let date2 = new Date();

    date1.setHours(0, 0, 0, 0);
    date2.setHours(0, 0, 0, 0);

    console.log("date1 => " + date1);
    console.log("date2 => " + date2);

    if (date1 > date2) {
        console.log("date1 is later than date2");
    } else if (date1 < date2) {
        console.log("date1 is earlier than date2");
    } else {
        console.log("date1 and date2 are the same");
    }
</script>

Output:

date1 => Wed Aug 28 2024 00:00:00 GMT+0000 (Coordinated Universal Time)
date2 => Wed Aug 28 2024 00:00:00 GMT+0000 (Coordinated Universal Time)
date1 and date2 are the same

Approach 2: Using toDateString() Method

In this approach, we use the toDateString() method to extract only the date portion of each Date object. The resulting strings can be compared to determine whether both dates fall on the same calendar day.

Syntax:

date1.toDateString() === date2.toDateString()

Example: Compare two dates while ignoring their time portions.

javascript
<script>
    let date1 = new Date(2024, 7, 28, 10, 30, 0);
    let date2 = new Date(2024, 7, 28, 18, 45, 0);

    if (date1.toDateString() === date2.toDateString()) {
        console.log("date1 and date2 are the same");
    } else {
        console.log("date1 and date2 are different");
    }
</script>

Output:

date1 and date2 are the same
Comment