The task is to determine if the given date is in between the given 2 dates or not? Here are a few of the most used techniques discussed with the help of JavaScript. In the first approach, we will use .split() method and the new Date() constructor. And in the second approach we will use the .getTime() method with the new Date() constructor.
Approach 1: Use .split() method to split the date on “/” to get the day, month and year in an array. The we have to construct the date from the array obtained in previous step for that we will use the new Date() constructor . Because this method returns the number of seconds from 1 Jan 1970, So it becomes easy to compare the dates.
- Example: This example uses the approach discussed above.
<!DOCTYPE HTML><html><head><title>How to Check if one Date is betweentwo dates using JavaScript ?</title><style>body {text-align: center;}h1 {color: green;}#geeks {font-weight: bold;}</style></head><body><h1>GeeksforGeeks</h1><p>Date 1 = "06/04/2019"Date 2 = "07/10/2019"<br>Date_to_check = "02/12/2019"</p><buttononclick="gfg_Run()">Click Here</button><pid="geeks"></p><script>var el_down = document.getElementById("geeks");// Format - MM/DD/YYYYvar Date_1 = "06/04/2019";var Date_2 = "07/10/2019";var Date_to_check = "02/12/2019";function gfg_Run() {D_1 = Date_1.split("/");D_2 = Date_2.split("/");D_3 = Date_to_check.split("/");var d1 = new Date(D_1[2], parseInt(D_1[1]) - 1, D_1[0]);var d2 = new Date(D_2[2], parseInt(D_2[1]) - 1, D_2[0]);var d3 = new Date(D_3[2], parseInt(D_3[1]) - 1, D_3[0]);if (d3 > d1 && d3 <d2) {el_down.innerHTML="Date is in between the "+ "Date 1 and Date 2";} else {el_down.innerHTML="Date is not in between "+ "the Date 1 and Date 2";}}</script></body></html>chevron_rightfilter_none - Output:

Approach 2: Here first use new Date() constructor and pass the string in it which makes a Date Object. The the .getTime() method which returns the number of seconds from 1 Jan 1970 and Seconds can be easily compared.
-
Example: This example uses the approach discussed above.
<!DOCTYPE HTML><html><head><title>How to Check if one Date is betweentwo dates using JavaScript ?</title><style>body {text-align: center;}h1 {color: green;}#geeks {font-weight: bold;}</style></head><body><h1>GeeksforGeeks</h1><p>Date 1 = "06/04/2019"Date 2 = "07/10/2019"<br>Date_to_check = "02/8/2019"</p><buttononclick="gfg_Run()">Click Here</button><pid="geeks"></p><script>var el_down = document.getElementById("geeks");// Format - MM/DD/YYYYvar D1 = "06/04/2019";var D2 = "07/10/2019";var D3 = "02/8/2019";function gfg_Run() {D1 = new Date(D1);D2 = new Date(D2);D3 = new Date(D3);if (D3.getTime() <= D2.getTime()&& D3.getTime() >= D1.getTime()) {el_down.innerHTML = "Date is in between"+ " the Date 1 and Date 2";} else {el_down.innerHTML = "Date is not in"+ " between the Date 1 and Date 2";}}</script></body></html>chevron_rightfilter_none - Output:

Recommended Posts:
- How to store all dates in an array present in between given two dates in JavaScript ?
- Return all dates between two dates in an array in PHP
- How to validate if input date (end date) in input field must be after a given date (start date) using express-validator ?
- How to validate if input date (start date) in input field must be before a given date (end date) using express-validator ?
- How to calculate minutes between two dates in JavaScript ?
- How to calculate the number of days between two dates in javascript?
- How to check the input date is equal to today's date or not using JavaScript ?
- Compare two dates using JavaScript
- Get the relative timestamp difference between dates in JavaScript
- How to convert UTC date time into local date time using JavaScript ?
- Program to find the number of days between two dates in PHP
- How to calculate the difference between two dates in PHP?
- How to select Min/Max dates in an array using JavaScript ?
- Comparing two dates in PHP
- How to get Month and Date of JavaScript in two digit format ?
- How to check a date is valid or not using JavaScript ?
- How to check if date is less than 1 hour ago using JavaScript ?
- Sort an array of dates in PHP
- How to get highcharts dates in the x-axis ?
- JavaScript | Adding minutes to Date object
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


