JavaScript Example | Compare Two Dates

This javascript example code will show us how to compare two dates.

<html>
<head>JavaScript Example | Compare Two Dates</head>
<body>
<script>
var d1=new Date("Apr 17, 2019 12:10:10"); //mm dd, yyyy hh:mm:ss
var d2=new Date("Dec 1, 2019 12:10:30"); //mm dd, yyyy hh:mm:ss
if(d1>d2)
{
document.write("False, d1 date and time is smaller than d2 date and time");
}
else if(d1<d2)
{
document.write("True, d2 is greater in terms of both time and date");
}
else
{
document.write("Both date and time are same and equal");
}
</script>
</body>
</html>

Run this code, we will get:

Comparing Date and time
True, d2 is greater in terms of both time and date