
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
How to compare dates in JavaScript?
Dates can be compared easily in the JavaScript the dates can belong to any frame like past, present, or future. Past dates can be compared to the future and the future can be compared to the past and present.
The date object will do what you want to construct one for each, then compare them using the ( >,<,<= or >= ). The ==, !=, === and !== operators require you to use date.getTime().
In JavaScript, we can compare two dates by converting them into numeric values to correspond to their time. First, we can convert the data into a numeric value by using the getTime() method by converting the given dates into numeric values we can directly compare them.
Example 1
In the following example, we are comparing the dates present date to provided date.
<!DOCTYPE html> <html> <head> <title>Compare the date with other date</title> </head> <body> <p id="compare"></p> <script> var today = new Date(); var otherday = new Date(); otherday.setFullYear(2022, 01, 19); if (otherday > today) { var date = "The date you provide is a future date"; } else { var date = "The date you provide is a past date"; } document.getElementById("compare").innerHTML = date; </script> </body> </html>
Example 2
In the following example, illustrate the comparison between dates with the help of the getTime() function.
<!DOCTYPE html> <html> <head> <title>Compare the date with other date</title> </head> <body> <p id="compare"></p> <script> var d1 = new Date(); var d2 = new Date(); if (d1.getTime() === d2.getTime()) document.write("Both are equal"); else document.write("Not equal"); </script> </body> </html>
Example 3
In the following example, the JavaScript code illustrates the comparison of two given dates.
<!DOCTYPE html> <html> <head> </head> <body> <script> var d1 = new Date(2019, 08, 03, 11, 45, 55); // (YYYY, MM, DD, Hr, Min, Sec) var d2 = new Date(2019, 08, 03, 11, 45, 55); if (d1.getTime() < d2.getTime()) document.write("d1 is lesser than d2"); else if (d1.getTime() > d2.getTime()) document.write("d1 is greater than d2"); else document.write("both are equal"); </script> </body> </html>
Example 4
In the following, we are comparing two dates but the logic behind this is day1 is automatically taking the current dates and on day2 we are passing the date.
Note − if we are passing the month value then it will be month value − 1 for example − if you thinking to pass the January month then the value will be 1−1 = 0, February value will be 1, etc. directly we can say that it is taking the value like an index of the array, 0,1,2,3,4….
<!DOCTYPE html> <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta http-equiv="X-UA-Compatible" content="IE=edge" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <title>date compare</title> </head> <body> <script> var day1 = new Date(); day1.setHours(0, 0, 0, 0); var day2 = new Date(2022, 06, 20); day2.setHours(0, 0, 0, 0); //here we are stoping the clock document.write("day1=>", day1); document.write("<br \>day2=>", day2); if (day1 > day2) { document.write("<br \>day1 is greater than day2"); } else if (day1 < day2) { document.write("<br \>day1 is older than day2"); } else { document.write("<br \>day1 and day2 are same"); } </script> </body> </html>
- Related Articles
- How to compare two dates with JavaScript?
- How to compare two dates in Java?
- How to compare two Dates in C#?
- Compare Dates in Java
- How do we compare Python Dates?
- How to compare two dates in String format in Java?
- How to compare two dates along with time in Java?
- PHP program to compare two dates
- How to compare dates if greater than another date in Excel?
- What are various ways to compare dates in Java?
- How to compare arrays in JavaScript?
- How to disable future dates in JavaScript Datepicker?
- How to compare two numbers in JavaScript?
- How to compare two objects in JavaScript?
- How to calculate minutes between two dates in JavaScript?
