JavaScript Compare Dates Without Time
In JavaScript, comparing dates involves comparing two Date objects to see which one comes before or after the other. So, in this article, we will see how to compare dates without time in javascript.
Date() object returns the date and time by default. So, we use the ‘toDateString()‘ function, which returns only the date.
var date1 = new Date(2020, 8, 20);
console.log(date1) // Sun Sep 20 2020 00:00:00 GMT+0530 (India Standard Time)
console.log( date1.toDateString()) //Sun Sep 20 2020
- In this example, we created Date object ‘date1’ and we set date only.
- Then we logged ‘date1’. See that, it returned the date and time. Here, the time is returned (00:00:00), because we didn’t set the time, so it returned the default value.
- We logged ‘date1’ with the ‘toDateString()’ function, it returned a date only.
- If we don’t pass a value to the Date() constructor, it will return the current date and time, like:
var date1 = new Date(); // it returns current data and time
3 Ways to compare dates without time
These are the main and easiest ways to compare dates:
1. Using getDate(), getMonth() & getFullYear() Methods:
The getDate() method returns the date, the getMonth() method returns the month, and the getFullYear() method returns the year. In this method, we get the date, month, and year and compare them separately.
var date1 = new Date();
var date2 = new Date(2010, 8, 20);
const date1WithoutTime = new Date(date1.getFullYear(), date1.getMonth(), date1.getDate());
const date2WithoutTime = new Date(date2.getFullYear(), date2.getMonth(), date2.getDate());
if (date1WithoutTime > date2WithoutTime) {
console.log('date1 is further than date2');
} else if (date1WithoutTime < date2WithoutTime) {
console.log('date1 is older than date2.');
} else {
console.log('date1 and date2 same.');
}
//date1 is further than date2
Steps:
- In this example, we have created two Date objects ‘date1’ and ‘date2’. These two objects represent two different dates and times.
- Then we create two new Date objects ‘date1WithoutTime’ and ‘date2WithoutTime’ using the getFullYear(), getMonth(), and getDate() methods of each original Date object. The ‘date1WithoutTime’ have a ‘date1‘ date without time and ‘date2WithoutTime’ have a ‘date2‘ date without time.
- We set the condition using the if statement, If ‘date1WithoutTime’ is greater than ‘date2WithoutTime’, meaning ‘date1’ is later than ‘date2’.
- If ‘date1WithoutTime’ is not greater than ‘date2WithoutTime’, the else if block is executed, which means “date1 is earlier than date2”.
- If ‘date1WithoutTime’ is not greater than or less than ‘date2WithoutTime’, meaning they are equal, the code inside the ‘else’ block is executed.
2. Use the toDateString():
In this method, we convert the date object into to string representation with only the date information. And then we compare string(date) easily.
var date1 = new Date();
var date2 = new Date(2020, 8, 20);
if (date1.toDateString() === date2.toDateString()) {
console.log('Same dates');
} else {
console.log('Different dates');
}
// Output: Different dates
Steps:
- In this above example code, we initialize a new Date object called “date1”, which represents the current date and time. We have passed no argument into the Date constructor, so it returns the current date and time on the user’s device.
- Then we initialize a new Date object called “date2”. It represents “September 20th, 2020 at 00:00:00 hours”. Because we passed into the Date constructor, the year (2020), month (0-based index, so 8 represents September), and day of the month (20). Since no arguments are passed for the time, so it returns the default “00:00:00” hours.
- We set the condition to print “same dates” if both dates are equal or “different dates” if not. The Date() defaulty returns the date and time. So, we use the “toDateString()” function to check the date only for ‘date1’ and ‘date2’. Then compare both dates using compare operator.
3. Using getTime() method:
The getTime() returns unix timestamp values. So, in this method, we compared the Unix timestamp of the two dates.
var date1 = new Date(2010, 8, 20);
var date2 = new Date(2010, 8, 20);
date1.setHours(0,0,0,0);
date2.setHours(0,0,0,0);
if (date1.getTime() > date2.getTime()) {
console.log('date1 is further than date2');
} else if (date1.getTime() < date2.getTime()) {
console.log('date1 is older than date2.');
} else {
console.log('date1 and date2 same.');
}
//Output: date1 and date2 same.
Steps:
- In this example, we have created two Date() objects ‘date1’ and ‘date2’.
- Then we set the default time to (0, 0, 0, 0) using setHours() methods. When we get unix timestamp values using the getTime() method, both dates are set to default time (0, 0, 0, 0).
- We set the condition using if/else statement, if “date1.getTime()” is greater than “date2.getTime()”, it returns “date1 is further than date2”.
- If “date1.getTime()” is less than “date2.getTime()”, it returns “date1 is older than date2.”.
- If both conditions are false, it returns logged “date1 and date2 same.”
- Here, both conditions are false, so it returns the output “same”.
Conclusion:
So, these are the best ways to compare dates without time in Javascript. I recommend the 1st method, I told in this article