Home >Web Front-end >JS Tutorial >How to check if a date is between two dates in JavaScript?
In JavaScript, we can use the Date() object to create different timestamps. Additionally, we may need to use JavaScript to check if a date is between two dates.
For example, we want to create a filter for orders in an e-commerce application based on date. So we should be able to filter all orders between two dates entered by the user in the date input field.
Another practical use case for checking between two dates is in banking applications. For example, when developing a banking system application, a developer needs to create a filter that allows the user to sort all transactions between two dates.
In JavaScript, we can compare two instances of date objects. When we compare two instances of Date objects, it compares the total number of milliseconds since January 1, 1970 to both dates.
So we can compare two dates normally just like comparing numeric values in JavaScript, and we can make sure that one date is between the other two dates.
Users can follow the following syntax to ensure that a date is between two dates in JavaScript.
if (date1 < date2 && date2 < date3) { // date2 is between the date1 and date3 } else { // date2 is not between the date1 and date3 }
In the above syntax, users can see that if date2 is greater than date1 and date2 is less than date3, it means date2 Between date1 and date3.
In the following example, we create three different timestamps using the constructor of the Date object. After that, we use the logic explained in the above syntax to check if date2 is between date1 and date3.
In the output, the user can observe that date2 is located between date1 and date3.
<html> <body> <h2><i>Comparing the dates</i> to check if one date is between two.</h2> <p id="output"> </p> <script> let output = document.getElementById("output"); let date1 = new Date(2020, 03, 11); let date2 = new Date(2022, 03, 12); let date3 = new Date(); if (date1 < date2 && date2 < date3) { output.innerHTML += date2 + " is between <br> " + date1 + " <br> and <br> " + date3; } else { output.innerHTML += date2 + " is not between the " + date1 + " <br> and <br>" + date3; } </script> </body> </html>
Now, let's consider a scenario where we are not given a timestamp as a standard date object, but the date is formatted as a string. So we have to extract the year, month, and day from the date string. After that, we need to create a standard timestamp for the value obtained from the string and compare them like we did in the previous section.
When given a randomly formatted string, the user can check whether a date is between two dates by following the following syntax.
// splitting the dates let [year1, month1, date1] = prev_date.split(","); let [year2, month2, date2] = current_date.split(","); let [year3, month3, date3] = final_date.split(","); // creating new formatted dates prev_date = new Date(year1, month1 - 1, date1); current_date = new Date(year2, month2 - 1, date2); final_date = new Date(year3, month3 - 1, date3); if (prev_date < current_date && current_date < final_date) { // current_date is between the prev_date and final_date } else{ // current_date is not between the prev_date and final_date }
In the above syntax, we use the ',' delimiter to split the string, but the user can split it based on the given date string. After that, we deconstruct the array obtained from the split method and create a new standard date timestamp using the value.
In this example, we take three date strings. Next, we separate them, get the year, month, and date values, and use them to create a new date.
Afterwards, we compare the new timestamps to ensure that current_date is between prev_date and Final_date.
<html> <body> <h3>Create a new date from <i> date string and compare them </i> to check if one date is between two</h3> <p id = "output"> </p> <script> let output = document.getElementById("output"); let prev_date = "2022,10,23"; let current_date = "2021,11,22"; let final_date = "2023,12,30"; let [year1, month1, date1] = prev_date.split(","); let [year2, month2, date2] = current_date.split(","); let [year3, month3, date3] = final_date.split(","); prev_date = new Date(year1, month1 - 1, date1); current_date = new Date(year2, month2 - 1, date2); final_date = new Date(year3, month3 - 1, date3); if (prev_date < current_date && current_date < final_date) { output.innerHTML += current_date + " is between <br>" + prev_date + "<br> and <br> " + final_date; } else { output.innerHTML += current_date + " is not between <br>" + prev_date + " <br> and <br> " + final_date; } </script> </body> </html>
Users learned two ways to check if a date is between two other dates. The user can use the first method when given a standard timestamp of a Date object; otherwise, the user can extract different values from the date string and use them to create new instances of the date object and compare them.
The above is the detailed content of How to check if a date is between two dates in JavaScript?. For more information, please follow other related articles on the PHP Chinese website!