javascript compare dates – How to compare two dates in JavaScript?

javascript compare dates – The ==, !=, ===, and !== operators require you to use date.getTime() as in Example

var startdate = new Date();
var enddate = new Date(startdate);
var same = startdate.getTime() === enddate.getTime();
var notSame = startdate.getTime() !== enddate.getTime();

javascript compare dates

there are different methods by which we can compare dates, like as: Comparing two dates with one another, Comparing date with time and Comparing dates using getTime() Examples.

Comparing two dates with one another

<html>  
<head> Comparing Dates</br></head>  
<body>  
<script>  
function compare()  
{  
var startdate=new Date('2022-01-25'); //yyyy-mm-dd  
var enddate=new Date('2022-01-25'); //yyyy-mm-dd  
if(startdate>enddate)  
{  
document.write("True, First date is greater than second date");  
}  
else if(startdate<enddate)  
{  
document.write("False, Second date is smaller than the first");  
}  
else  
{  
document.write("Both date are same and equal");  
}  
}  
compare(); //invoking compare()  
</script>  
</body>  
</html>  

Comparing date with time

<html>  
<head> Comparing Date and time</br></head>  
<body>  
<script>  
var startdate=new Date("Apr 17, 2025 12:10:10"); //mm dd, yyyy hh:mm:ss  
var enddate=new Date("Dec 1, 2025 12:10:30"); //mm dd, yyyy hh:mm:ss  
if(startdate>enddate)  
{  
document.write("False, startdate date and time is smaller than enddate date and time");  
}  
else if(startdate<enddate)  
{  
document.write("True, enddate is greater in terms of both time and date");  
}  
else  
{  
document.write("Both date and time are same and equal");  
}  
</script>  
</body>  
</html>  

Example2: Comparing same dates with disimilar timings

<html>  
<head> Comparing same date but different time</br></head>  
<body>  
<script>  
var startdate=new Date("2022-01-10, 12:10:10"); //yyyy-mm-dd hh:mm:ss  
var enddate=new Date("2022-01-10, 12:10:50"); //yyyy-mm-dd hh:mm:ss  
if(startdate>enddate)  
{  
document.write("False, startdate & enddate dates are same but enddate time is greater than startdate time");  
}  
else if(startdate<enddate)  
{  
document.write("True, enddate time is greater than startdate time.");  
}  
else  
{  
document.write("Both date and time are same and equal");  
}  
</script>  
</body>  
</html>  

Compare two dates with JavaScript

<script>
	// Current Date
	var startdate = new Date();
	var enddate = new Date();
	if (startdate.getTime() === enddate.getTime())
		document.write("Both are equal");
	else
		document.write("Not equal");
	javascript: ;
</script>

javascript compare dates Function

function dateCompare(d1, d2){
    const startdate = new Date(d1);
    const enddate = new Date(d2);

    if(startdate > enddate){
        console.log(`${d1} is greater than ${d2}`)
    } else if(startdate < enddate){
        console.log(`${d2} is greater than ${d1}`)
    } else{
        console.log(`Both dates are equal`)
    }
}

dateCompare("6/11/2022", "7/8/2019")
dateCompare("01/01/2023", "01/01/2023")

Don’t Miss : How To Get Difference Between Two Dates In Javascript?

I hope you get an idea about javascript compare dates.
I would like to have feedback on my infinityknow.com.
Your valuable feedback, question, or comments about this Article are always welcome.
If you enjoyed and liked this post, don’t forget to share.