Compare two dates in javascript yyyy/mm/dd; In this tutorial, you will learn how to compare two dates with javascript. Learn different techniques to compare 2 dates in javascript with examples.
Compare two dates in JavaScript
This tutorial will take several examples for comparing two dates in javascript with time, without time and date and current date:
- First Method – Compare two dates with JavaScript
- Second Method – compare date with current date in javascript
- Third method – javascript compare dates with time
- Fourth Method – javascript compare two dates with time
First Method – Compare two dates with JavaScript
You can use the below javascript code for comparing two dates without time:
var date1 = new Date('2019-06-27'); var date2 = new Date('2019-05-25'); if(date1>date2){ document.write('date1>date2'); } else if(date1<date2) { document.write('date1<date2'); } else{ document.write('false'); }
Ex:-
<!DOCTYPE html> <html> <head> <title>Compare two dates javascript</title> </head> <body> <script type="text/javascript"> var date1 = new Date('2019-06-27'); var date2 = new Date('2019-05-25'); if(date1>date2){ document.write('date1>date2'); } else if(date1<date2) { document.write('date1<date2'); } else{ document.write('false'); } </script> </body> </html>
Result of the above code is: date1>date2
Second Method – compare date with current date in javascript
Let’s take the second example for compare date with the current date in javascript:
var currentDate = new Date(); var date = new Date('2019-12-28'); if(currentDate>date){ document.write('currentDate > date'); }else if(currentDate<date) { document.write('currentDate < date'); } else{ document.write('currentDate==date'); }
Ex:-
<!DOCTYPE html> <html> <head> <title>compare date with current date in javascript</title> </head> <body> <script type="text/javascript"> var currentDate = new Date(); var date = new Date('2019-12-28'); if(currentDate>date){ document.write('currentDate > date'); }else if(currentDate<date) { document.write('currentDate < date'); } else{ document.write('currentDate==date'); } </script> </body> </html>
Result of the above code is: currentDate < date
Third method – javascript compare dates with time
Let’s take an example to compare two dates with time:
var date1 = new Date( "Dec 18, 2019 20:40:45" ); var date2 = new Date( "Nov 15, 2018 21:35:40" ); if(date1>date2){ document.write('date1 > date2'); }else if(date1<date2) { document.write(' date1 < date2 '); } else{ document.write(' date1 == date2 '); }
Ex:-
<!DOCTYPE html> <html> <head> <title>javascript compare dates with time</title> </head> <body> <script type="text/javascript"> var date1 = new Date( "Dec 18, 2019 20:40:45" ); var date2 = new Date( "Nov 15, 2018 21:35:40" ); if(date1>date2){ document.write('date1 > date2'); }else if(date1<date2) { document.write(' date1 < date2 '); } else{ document.write(' date1 == date2 '); } </script> </body> </html>
Result of the above code is: date1 > date2
Fourth Method – javascript compare two dates with time
To compare two dates using the getTime() method:
var d1 = new Date( " Wed Nov 26 2019 09:52:06 " ); var d2 = new Date(); document.write("date 1 is :- " + d1); document.write("<br>"); document.write("date 2 is :- " + d2); document.write("<br>"); if (d1.getTime() === d2.getTime()) document.write("Both are equal"); else document.write("Not equal");
Ex:-
var d1 = new Date( " Wed Nov 26 2019 09:52:06 " ); var d2 = new Date(); document.write("date 1 is :- " + d1); document.write("<br>"); document.write("date 2 is :- " + d2); document.write("<br>"); if (d1.getTime() === d2.getTime()) document.write("Both are equal"); else document.write("Not equal");
Result of above code is:
date 1 is :- Tue Nov 26 2019 09:52:06 GMT+0530 (India Standard Time) date 2 is :- Wed Nov 27 2019 09:52:50 GMT+0530 (India Standard Time) Not equal
If you want to know more about javascript date and time methods, you may like following date and time methods:
Recommended JavaScript Tutorials
If you have any questions or thoughts to share, use the comment form below to reach us.