Javascript Check if Date is Between Two Dates

Javascript Check if Date is Between Two Dates | The goal is to determine if the supplied date falls between the two dates. Here are some of the most common strategies explained using JavaScript. We’ll use the split() method and the new Date() function Object() in the first technique. Next, we’ll use a simple if-else loop approach. Let us look at a few examples that will help you understand the topic, just a bit better.

Example-1 (Date format YYYY-mm-dd):
Start date: 2001-10-01
End date: 2002-10-01
To check: 2002-09-13
Output: It is between the given two dates.

Example-2 (Date format YYYY-mm-dd):
Start date: 2022-01-30
End date: 2073-12-01
To check: 2009-03-15
Output: It is not between the two given dates.

Javascript Check if Date is Between Two Dates Using split() method

You can supply two types of parameters to the Date() function Object() if you’re having trouble producing a legitimate Date object from your date strings. The date string is in the format mm/dd/YYYY, however, the method works with any format.

When we passed the month to the Date() function Object(), we deducted one because the Date function Object() expects a zero-based number, such as January = 0, February = 1, March = 2, and so on, this is the case. To get an array of substrings, we split the string on each forward slash.

Javascript Check if Date is Between Two Dates Using split() method

const dateStr = '09/11/2021';
const [month1, day1, year1] = dateStr.split('/');
const date = new Date(+year1, month1 - 1, +day1);

const startStr = '03/12/2021';
const [month2, day2, year2] = startStr.split('/');
const startDate = new Date(+year2, month2 - 1, +day2);

const endStr = '13/09/2022';
const [month3, day3, year3] = endStr.split('/');
const endDate = new Date(+year3, month3 - 1, +day3);

if (date > startDate && date < endDate) {
   console.log('The date is between the given two dates');
} else {
   console.log('Date is not between the given two dates');
}

Output:-

The date is between the given two dates

We assigned the month, day, and year data to variables and gave them to the Date() function Object() using array restructuring because the Date objects are changed to a timestamp well before comparison, you can simply compare the dates as you would integers once you’ve produced Date objects from the current strings.

To extract the month, day, and year values, we did this twice for each of the dates, splitting them on the forward slashes. Because the Date() function Object() requires a zero-based number for the month (January = 0, February = 1, March = 2), it’s critical to deduct 1 from the month’s value because the date falls between the start and finish dates, the if block from the previous example runs.

Javascript Check if Date is Between Two Dates Using && and if-else

To see if a date is between two others, do the following:-

1. To convert dates to Date objects, use the Date() function Object() 
2. Check to see if the current date is later than the start date but earlier than the end date.
3. The date is between the two dates if both conditions are met.

Javascript Check if Date is Between Two Dates Using && and if-else

const date = new Date('2010/10/10');

const start = new Date('2001/04/16');
const end = new Date('2009/01/01');

if (date > start && date < end) {
   console.log('The date is between the given two dates');
} else {
   console.log('The date is not between the given two dates');
}

Output:

The date is not between the given two dates

To build Date objects, we gave the date strings to the Date() function Object(). If the Date() function Object() does not provide a valid date, then you must format your date strings appropriately, for example, YYYY-mm-dd.

Because each date saves a timestamp behind the hood, even if you don’t specifically call the getTime() method on each date, the default behavior is to check the timestamps of the dates.

Thus, we saw Javascript Check if Date is Between Two Dates. Also see:- Difference Between Switch And If Else In JavaScript

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

Your email address will not be published.