Compare two dates with JavaScript. Here we will learn, how to compare two dates with javascript. Learn different techniques to compare 2 dates in javascript with examples.
Compare two dates with JavaScript
In this tutorial, we 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
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
Here we will take example 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:
You may like
- JavaScript Get Month Name With Various Examples
- Get Month 2 Digits in JavaScript
- javaScript Get Current Year 2 and 4 Digit – Example
- Get Current Date Time javaScript
- JavaScript: Date setUTCDate() Method
- Set UTC Month In javaScript
- setUTCFullYear() Method JavaScript With Examples
- javascript date setUTCHours() Method With Examples
- JavaScript: d.setUTCMinutes() Method e.g.
- setUTCSecond() Method By JavaScript
- javaScript Date set UTC milliseconds
- setUTCSecond() Method By JavaScript
- JavaScript: Date.getUTCDate() Method
- getUTCmonth Method javaScript With Example
- Digital Clock with date in javaScript
- JavaScript: Set Date Methods
- JavaScript Get Date Methods
If you have any questions or thoughts to share, use the comment form below to reach us.