In JavaScript, we can compare two dates by converting them into numeric value to corresponding to its time. First, we can convert the Date into a numeric value by using getTime() function. By converting the given dates into numeric value we can directly compare them.
Example-1:
<script> // Current Date var g1 = new Date(); var g2 = new Date(); if (g1.getTime() === g2.getTime()) document.write("Both are equal"); else document.write("Not equal"); javascript: ; </script> |
chevron_right
filter_none
Output:
Both are equal
Example-2:
<script> var g1 = new Date(); // (YYYY-MM-DD) var g2 = new Date(2019 - 08 - 03); if (g1.getTime() < g2.getTime()) document.write("g1 is lesser than g2"); else if (g1.getTime() > g2.getTime()) document.write("g1 is greater than g2"); else document.write("both are equal"); javascript: ; </script> |
chevron_right
filter_none
Output:
g1 is greater than g2
Example-3:
<script> var g1 = new Date(2019, 08, 03, 11, 45, 55); // (YYYY, MM, DD, Hr, Min, Sec) var g2 = new Date(2019, 08, 03, 10, 22, 42); if (g1.getTime() < g2.getTime()) document.write("g1 is lesser than g2"); else if (g1.getTime() > g2.getTime()) document.write("g1 is greater than g2"); else document.write("both are equal"); javascript: ; </script> |
chevron_right
filter_none
Output:
g1 is greater than g2
Recommended Posts:
- How to store all dates in an array present in between given two dates in JavaScript ?
- Return all dates between two dates in an array in PHP
- Compare Dates in Java
- How to compare two JavaScript array objects using jQuery/JavaScript ?
- How to calculate minutes between two dates in JavaScript ?
- How to calculate the number of days between two dates in javascript?
- How to check if one date is between two dates in JavaScript ?
- How to select Min/Max dates in an array using JavaScript ?
- How to compare two arrays in JavaScript?
- Get the relative timestamp difference between dates in JavaScript
- Program to find the number of days between two dates in PHP
- How to calculate the difference between two dates in PHP?
- Comparing two dates in PHP
- Calculating total number of Hours, Days, Minutes and Seconds between two dates in Golang
- Compare the Case Insensitive strings in JavaScript
- Optimum way to compare strings in JavaScript
- How to compare date part only without comparing time in JavaScript?
- How to compare password and confirm password inputs using express-validator ?
- Sort an array of dates in PHP
- How to get highcharts dates in the x-axis ?
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


