How to compare date part only without comparing time in JavaScript?
Last Updated :
29 Aug, 2024
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");
}
Outputdate1 => 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");
}
Outputdate1 => 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
Similar Reads
How to calculate the yesterday's date in JavaScript ?
In this article, we will see how to calculate yesterdayâs date in JavaScript. To calculate yesterday's date, you need to have some basic ideas of a few methods of JavaScript. JavaScript getDate() MethodJavaScript setDate() MethodJavaScript getDate() Method: It is an inbuilt JavaScript function that
3 min read
How to calculate minutes between two dates in JavaScript ?
Given two dates and the task is to get the number of minutes between them using JavaScript. Approach: Initialize both Date object.Subtract the older date from the new date. It will give the number of milliseconds from 1 January 1970.Convert milliseconds to minutes. Example 1: This example uses the c
2 min read
How to get the first day of the year in JavaScript ?
Given a date/year and the task is to get the first day of the year using JavaScript. Approach 1: Use getFullYear() Method to get the year from the given date.Use the new Date() function to create the new date object using year, month, and day. Example: This example uses the getFullYear() method to g
1 min read
How to remove time from date using JavaScript ?
Given a Date Object and the task is to remove the time portion from the object using JavaScript. JavaScript split() Method: This method is used to split a string into an array of substrings, and returns the new array. Syntax: string.split( separator, limit ) Parameters: separator: This parameter is
2 min read
How to get the current date and time in seconds ?
In this article, we will learn how to get the current date & time in seconds using JavaScript build-in methods. We will perform it in 2 ways: Using Date.now() methodUsing new Date.getTime() methodMethod 1: Using Date.now() MethodThe Date.now() method returns the number of milliseconds elapsed si
2 min read
How to get Month and Date of JavaScript in two digit format ?
To get the Month and Date of JavaScript in two-digit format, we have multiple approaches. In this article, we are going to learn how to get the Month and Date of JavaScript in a two-digit format. Below are the approaches used to get the Month and Date of JavaScript in two-digit format: Table of Cont
3 min read
How to Check Whether an Object is a Date ?
This article will show you how to check whether the given object is a Date or not. There are two methods to check for date objects, which are described below: Method 1: Using instanceof Operator The instanceof operator checks whether the prototype property of a constructor appears anywhere in the pr
2 min read
How to Get the Current Date in JavaScript ?
The Date() object is used to access the current date in JavaScript. To get the current date in string format, we can use toDateString() method. The date.toDateString() method converts the given date object into the date portion into a string. Syntax dateObj.toDateString()[GFGTABS] JavaScript // Crea
2 min read
How to convert a JavaScript date to UTC?
Javascript date can be converted to UTC by using functions present in the Javascript Date object. The toUTCString() method is used to convert a Date object into a string, according to universal time. The toGMTString() returns a string that represents the Date based on the GMT (UT) time zone. By defa
1 min read
How to get the first and last date of current month using JavaScript ?
Given a month and the task is to determine the first and last day of that month in proper format using JavaScript. JavaScript getDate() Method: This method returns the day of the month (from 1 to 31) for the defined date. Syntax: Date.getDate() Parameters: This method does not accept any parameters.
3 min read