How to check if one date is between two dates in JavaScript?

In JavaScript, we can use the Date() object to create different timestamps. Also, we may be required to check if one date is between two using JavaScript.

For example, we want to create a filter for orders in the eCommerce application based on the dates. So, we should be able to filter all orders between the two dates that users enter into the date input field.

Another real-world use case of checking one date between two is in the banking application. For example, while developing the banking system application, developers need to create a filter allowing users to sort all transactions between two dates.

Method 1: Direct Date Comparison

In JavaScript, we can compare the two instances of the date object. When we compare the two instances of the Date object, it compares the total milliseconds till both dates starting from 1st January 1970.

So, we can compare two dates normally as we compare number values in JavaScript and can ensure that one date is between the other two dates.

Syntax

if (date1 

In the above syntax, users can see that if date2 is greater than date1 and date2 is less than date3, that means date2 is between date1 and date3.

Example

In the example below, we have created three different timestamps using the Date object's constructor. After that, we used the logic explained in the above syntax to check if the date2 is 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>
Tue Apr 12 2022 00:00:00 GMT+0000 (UTC) is between 
Sat Apr 11 2020 00:00:00 GMT+0000 (UTC) 
and 
Mon Dec 16 2024 15:30:45 GMT+0000 (UTC)

Method 2: Using Date Strings with Parsing

Now, let's think about the scenario in which we haven't given the timestamp of the standard date object, but the date is formatted in the string format. So, we have to extract the year, month, and day from the date string. After that, we need to create a standard timestamp of the values we got from the string and compare them.

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 

In the above syntax, we have used the ',' delimiter to split the string, but users can take it according to the given date string format. After that, we destructured the array that we got from the split method and used those values to create new standard date timestamps.

Example

In this example, we have taken three date strings. Next, we split them, got the year, month, and date values, and used them to create new dates.

<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>
Mon Nov 22 2021 00:00:00 GMT+0000 (UTC) is not between
Sun Oct 23 2022 00:00:00 GMT+0000 (UTC) 
and 
Sat Dec 30 2023 00:00:00 GMT+0000 (UTC)

Key Points

  • JavaScript Date objects can be directly compared using comparison operators (<, >, <=, >=)
  • Month values in the Date constructor are zero-indexed (January = 0, February = 1, etc.)
  • Always subtract 1 from the month when creating dates from parsed strings
  • Date comparison works by comparing the underlying millisecond values

Conclusion

JavaScript provides simple date comparison using standard operators. Use direct comparison for Date objects, or parse string dates first. Both methods effectively determine if one date falls between two others.

Updated on: 2026-03-15T23:19:01+05:30

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements