Open In App

How to compare date part only without comparing time in JavaScript?

Last Updated : 29 Aug, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

Comparing the date part only without comparing time in JavaScript means checking if two dates fall on the same calendar day, ignoring the specific time (hours, minutes, seconds). This is useful when you want to know if two events occur on the same day, regardless of their time.

To obtain the date and time in JavaScript, we have the Date() class. We access the Date() class by creating its objects. Date() class returns date and time both combined.

There are four ways of instantiating a date:

Syntax:

let d = new Date();
//returns the present date and time

or

let d = new Date(milliseconds);
//to set a specific date

or

let d = new Date(dateString);
//to set a specific date

or

let d = new Date(year, month, day, hours, minutes, seconds, milliseconds);
//to set a specific date:

So the Date() returns function Date() { [native code] } and we can turn that into a string by using object.toString()

Approach 1: JavaScript Date() Class

When the objects get compared it gets compared both with time and date.

So now we gonna make the time to 00:00:00(stop) in that way when the objects get compared only the date gets compared as the time for both date objects will be 00:00:00(stop).

To set the time as 00:00:00(stop), setHours() comes in handy.

Syntax;

//the hour(0-23) is mandatory.
dateobject.setHours(hour, min, sec, millisec)

And for making the time as 0 or completely stopped, we have to make sure that all the parameters are 0 including milli-seconds.

Example: In this example we compares dates for differences. It sets hours to midnight for accuracy and compares date values, printing results to the console to show which date is earlier or later.

javascript
// Example-1
let date1 = new Date();
date1.setHours(0, 0, 0, 0);
let date2 = new Date(2020, 8, 20); // Month is zero-based (September)
date2.setHours(0, 0, 0, 0);
console.log("date1 => " + date1);
console.log("date2 => " + date2);
if (date1 > date2) {
    console.log("date1 is further than date2");
} else if (date1 < date2) {
    console.log("date1 is older than date2");
} else {
    console.log("date1 and date2 are the same");
}

// Example-2
date2 = new Date(2019, 11, 3); // December
console.log("\ndate1 => " + date1);
console.log("date2 => " + date2);
if (date1 > date2) {
    console.log("date1 is further than date2");
} else if (date1 < date2) {
    console.log("date1 is older than date2");
} else {
    console.log("date1 and date2 are the same");
}

// Example-3
date1 = new Date();
date2 = new Date();
date1.setHours(0, 0, 0, 0);
date2.setHours(0, 0, 0, 0);
console.log("\ndate1 => " + date1);
console.log("date2 => " + date2);
if (date1 > date2) {
    console.log("date1 is further than date2");
} else if (date1 < date2) {
    console.log("date1 is older than date2");
} else {
    console.log("date1 and date2 are the same");
}

// Example-4
date1 = new Date();
date2 = new Date();
date1.setHours(0, 0, 0, 0);
// Uncomment the line below to compare without time part
// date2.setHours(0, 0, 0, 0);
console.log("\ndate1 => " + date1);
console.log("date2 => " + date2);
if (date1 > date2) {
    console.log("date1 is further than date2");
} else if (date1 < date2) {
    console.log("date1 is older than date2");
} else {
    console.log("date1 and date2 are the same");
}

Output
date1 => Wed Aug 28 2024 00:00:00 GMT+0000 (Coordinated Universal Time)
date2 => Sun Sep 20 2020 00:00:00 GMT+0000 (Coordinated Universal Time)
date1 is further than date2

date1 => Wed Aug 28 2024 00:00:00 GMT+0000 (Coordinated Universal Time)
date2 => Tue Dec 03 2019 00:00:00 GMT+0000 (Coordinated Universal Time)
date1 is further than date2

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

date1 => Wed Aug 28 2024 00:00:00 GMT+0000 (Coordinated Universal Time)
date2 => Wed Aug 28 2024 11:56:35 GMT+0000 (Coordinated Universal Time)
date1 is older than date2

Approach 2: Using toDateString()

Here we gonna use toDateString() to obtain the only date, but when obtaining it gets converted to a string, as the comparison is not possible for strings, we need to get then again converted to a date object, now when converting them back to object time sets to 00:00:00(stops).

Syntax:

let date1 = new Date(new Date().toDateString());

Breakdown of the above syntax

new Date().toDateString()
converts the Date object to a string containing only the date part.
new Date(new Date().toDateString()); 
converts it back to Date object setting time to 00:00:00

Example: In this example we compares two dates: date1 (today’s date) and date2 (September 20, 2018). It logs their values and checks if date1 is further, older, or the same as date2.

javascript
// Example-1
let date1 = new Date(new Date().toDateString());
let date2 = new Date(new Date(2018, 8, 20).toDateString());

console.log("date1 => " + date1); // returns only the date
console.log("date2 => " + date2);

if (date1 > date2) {
    console.log("date1 is further than date2");
} else if (date1 < date2) {
    console.log("date1 is older than date2");
} else {
    console.log("date1 and date2 are the same");
}

Output
date1 => Wed Aug 28 2024 00:00:00 GMT+0000 (Coordinated Universal Time)
date2 => Thu Sep 20 2018 00:00:00 GMT+0000 (Coordinated Universal Time)
date1 is further than date2


Next Article

Similar Reads