Searching an Array of Dates with Javascript

Photo by Curtis MacNewton on Unsplash

Scenario

const dates = [new Date(2014, 11, 25), new Date(2014, 11, 24), new Date(2014, 11, 25)];

Approach


let result = []
if (!isNaN(new Date(input).getTime())) {
result = dates.filter(date=> {
const day = date.getDate();
const monthIndex = date.getMonth();
const year = date.getFullYear();
const dateString = `${monthIndex}/${day}/${year}`;
return dateString.includes(input);
});
}
console.log("The dates that match are: ", result);
console.log("Two dates match: ", result.length == 2);

Demo

Brought To You By

--

--

Founder of Firefly Semantics Corporation

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store