I want to check an array of dates to see if they are increasing. For example, [’12/12/2023′,’13/12/2023′] would return true. However, [’12/12/2023′,’11/12/2023′] would return false.
I have been playing around with a function suggested by users for checking integer values. I thought I could do the same but use dates instead.
Here is the function:
function isIncreasing(dates) {
for (var i = 1; i < dates.length; i++) {
let convertedToDate = new Date(dates[i]);
//console.log(currentDate);
if (convertedToDate[i] !== convertedToDate[i - 1] && convertedToDate[i] != convertedToDate[i - 1] + 1) {
return false;
}
}
return true;
}
console.log(isIncreasing(['12/12/2023','11/12/2023','10/12/2023']))
The function does not work as the above returns true. Any help is appreciated.
2
Answers
I managed to resolve it by converting the date to a timestamp before comparing. This works if the date is in the format (Y-m-d). Here is the final:
Just split the dates by
/
and compare the numbers: