I would like to check does the date fall within the range. I create two functions, first func transforms type str to Date,second func must find result.
let probeArray = [{price: 123, date: '2021-11-27'},
{price: 13, date: '2021-11-15'},
{price: 1, date: '2021-10-2'},
{price: 17, date: '2021-10-1'}];
let startDate = '2021-10-1';
let endDate = '2021-10-20';
// transform str to Date
const toDate = (dateStr) => {
const [year,month,day] = dateStr.split("-");
// console.log('check date')
// console.log([day, month, year])
return new Date(year, month - 1, +day+1);
}
function get_staticstic(probeAr, start, end){
let result = null;
let maxDate = toDate(start);
let minDate = toDate(end);
for (let tempEvent of probeAr){
let currentDate = toDate(tempEvent.date);
console.log('maxDate', maxDate);
console.log('minDate', minDate);
console.log('currentDate',currentDate);
if (currentDate >= minDate && currentDate <= maxDate ){
console.log('Correct Date');
}
else{
console.log('Out Side range!!');
}
}
return result
}
get_staticstic(probeArray, startDate, endDate);
But after start result for all dates is 'Out Side range!!'.

minDateandmaxDatemixed up inget_statistics