How To Check If A Date Is Between Two Dates In JavaScript

Check If A Date Is Between Two Dates In JavaScript

In this tutorial, we will show you how to check if a date is between two dates in JavaScript. We can use the Date() or split() method. Let’s start now.

Check if a date is between two dates in JavaScript

Right now, I will present to you the most simple and understandable method to check if a date is between two dates in JavaScript.

Using the Date() method

If you do not know the syntax and usage of the Date() function, you can return to this article How To Get A UTC Timestamp Using JavaScript – LearnShareIT to see.

Here, we use the Date() method to deal with dates. If the Date we need to check is greater than the start date and less than the end date, we affirm that the Date is between two dates or vice versa; it is not between two dates. See code example below:

Example 1: A date is between two dates

// Initialize the date need to check
const check_date = new Date('2022-10-16');

// Initialize start date
const start_date = new Date('2022-10-01');

// Initialize end date
const end_date = new Date('2022-10-31');

// Check if 'check_date' is between two dates and print the result
if (check_date > start_date && check_date < end_date) {
  	console.log('The date is between two dates');
} else {
  	console.log('The date is not between two dates');
}

Output:

The date is between two dates

Example 2: A date is not between two dates

// Initialize the date need to check
const check_date = new Date('2022-10-16');

// Initialize start date
const start_date = new Date('2022-10-01');

// Initialize end date
const end_date = new Date('2022-10-10');

// Check if date is between two dates and print the result
if (check_date > start_date && check_date < end_date) {
  	console.log('The date is between two dates');
} else {
  	console.log('The date is not between two dates');
}

Output:

The date is not betwee two dates

Using the split() method

The split() method splits a string into an array of substrings with a separator.

Syntax:

Parameters:

  • sep: A string or standard articulation to use for splitting.
  • limit: Number that restricts the quantity of splits.

Return: A new array with splitted values.

Here, we will initialize the Date with the format DD/MM/YYYY and we will use the split() method to split the string into an array, then compare to check if a date is between two dates.

Example 1: A date is between two dates

// Initialize the dates
var start_date = "16/10/2022";
var end_date = "01/10/2022";
var check_date = "31/10/2022";

// Split a string into array
var start_dates = start_date.split("/");
var end_dates = end_date.split("/");
var check_dates = check_date.split("/");

var start = new Date(start_dates);  // -1 because months are from 0 to 11
var end = new Date(end_dates);
var check = new Date(check_dates);

// Check condition
if (check > start && check < end) {
  	console.log('The date is between two dates');
} else {
  	console.log('The date is not between two dates');
}

Output:

The date is between two dates

Example 2: A date is not between two dates

// Initialize the dates
var start_date = "16/10/2022";
var end_date = "01/10/2022";
var check_date = "10/10/2022";

// Split a tring into array
var start_dates = start_date.split("/");
var end_dates = end_date.split("/");
var check_dates = check_date.split("/");

var start = new Date(start_dates);  // -1 because months are from 0 to 11
var end = new Date(end_dates);
var check = new Date(check_dates);

// Check condition
if (check > start && check < end) {
  	console.log('The date is between two dates');
} else {
  	console.log('The date is not between two dates');
}

Output:

The date is not between two dates

Summary

Above are the easiest methods, hope you will find the right way to check if a date is between two dates in JavaScript. If you have any questions, please leave a comment below. I will answer as possible. Thanks for reading!

Maybe you are interested:

Leave a Reply

Your email address will not be published. Required fields are marked *