Skip to main content

How to Check if a Date is in an Array in JavaScript

Checking if an array contains a specific value is a common task, but when working with Date objects, it's not as simple as using the Array.prototype.includes() method. This is because Dates are objects, and JavaScript's default comparison checks for object identity, not for equivalent date and time values.

This guide will explain why direct comparison fails and teach you the correct methods to solve this problem. You will learn how to use the .find() method with a timestamp comparison for an exact match, and how to use .toDateString() when you only need to check if a specific day is in the array, regardless of the time.

The Core Problem: Object Identity vs. Value

The Array.prototype.includes() method uses a "Same-Value-Zero" comparison, which for objects, means it checks if they are the exact same object in memory. Two different Date objects that represent the exact same moment in time are still considered different objects.

Example that does not work:

const date1 = new Date('2023-10-27');
const date2 = new Date('2023-10-27');
const dateArray = [date1];

console.log(date1 === date2); // Output: false (because they are different objects)

// This will NOT find the date, even though the values are the same.
console.log(dateArray.includes(date2)); // Output: false
note

Because of this behavior, you cannot use simple methods like includes() or indexOf() to find a date in an array.

Solution 1: Check for an Exact Date and Time Match

The correct way to check if two Date objects represent the same moment is to compare their primitive value, which is the number of milliseconds since the Unix Epoch. You can get this value using the .getTime() method.

The Array.prototype.find() method is perfect for this. It allows you to provide a custom function to test each element.

const dateArray = [
new Date('2023-10-27T10:00:00'),
new Date('2023-11-15T12:30:00'),
new Date('2023-12-01T15:00:00'),
];

const dateToFind = new Date('2023-11-15T12:30:00');

// Use .find() to iterate over the array
const foundDate = dateArray.find(date => {
// Compare the millisecond timestamps of the two dates
return date.getTime() === dateToFind.getTime();
});

if (foundDate) {
console.log('Date was found in the array:', foundDate);
} else {
console.log('Date was not found.');
}

Output:

Date was found in the array: 2023-11-15T12:30:00.000Z

If .find() finds a match, it returns the matching Date object from the array. If no match is found, it returns undefined.

Solution 2: Check if a Day is in the Array (Ignoring Time)

Often, you don't care about the exact hour, minute, and second. You just want to know if a specific day is present in the array. For this, you can convert both dates to a standardized date-only string before comparing them. The .toDateString() method is ideal for this.

const dateArray = [
new Date('2023-10-27T10:00:00'),
new Date('2023-11-15T12:30:00'), // This has a time component
];

const dateToFind = new Date('2023-11-15T00:00:00'); // A different time on the same day

// Use .find() and compare the .toDateString() output
const foundDate = dateArray.find(date => {
return date.toDateString() === dateToFind.toDateString();
});

if (foundDate) {
console.log('A date on that day was found:', foundDate);
} else {
console.log('No date on that day was found.');
}

Output:

A date on that day was found: 2023-11-15T12:30:00.000Z

How the Methods Work

  • .getTime(): This method returns the primitive value of a Date object—a number. Comparing two numbers (1672531200000 === 1672531200000) is a reliable way to check for value equality.
  • .toDateString(): This method returns a human-readable string representing only the date portion (e.g., "Fri Oct 27 2023"). By comparing these generated strings, you can effectively check if two Date objects fall on the same day, regardless of their time components.

Conclusion

Because Dates are objects, you cannot use simple array methods like .includes() to find a specific date.

Instead, you must use a method that allows for a custom comparison:

  1. To find a date with an exact time match, use array.find() and compare the millisecond timestamps of the dates using .getTime().
  2. To find a date on a specific day (ignoring the time), use array.find() and compare the string outputs of .toDateString().