I have to iterate on multiple dates intervals: -for example:
- 09/06/2023 - 15/06/2023
- 28/05/2023 - 02/06/2023
- 17/06/2023 - 18/06/2023
- 29/06/203 - 04/07/2023 ...etc
I need to obtain day numbers for only one given month: in this case June (06).
In theory I should obtain an array like this: var june = [09,10,11,12,13,14,15,01,02,17,18,29,30];
Steps taken by me:
a. in php query to take all departing, returning dates and number of date intervals as arrays (they are string Arrays, not Date object, integer in case of date interval numbers)
b. create arrays of departing and returning dates in javascript: var departure, var rback, var number
c. define empty array of days:
var days=[];
d. loop through all date intervals in order to obtain all dates in between the intervals
function enumerateDaysBetweenDates(startDate, endDate) {
startDate = moment(startDate,"DD/MM/YYYY");
endDate = moment(endDate,"DD/MM/YYYY");
var now = startDate, dates = [];
while (now.isBefore(endDate) || now.isSame(endDate)) {
dates.push(now.format("DD/MM/YYYY"));
now.add(1, 'days');
}
return dates;
};
for (i = 0; i < number.length; i++) {
var mdepart=departure[i];
var mrback=rback[i];
days.push(enumerateDaysBetweenDates(mdepart,mrback));
};
Now I need to filter all dates that are not in June:
function checkd(num) {
return num.includes("/06/");
};
var june=days.filter(checkd);
The problem: when I run this I obtain an error "days.filter is not a function"...
If I run it like this: var june = Object.values(days).filter(checkd);
I have an empty array...
I don't know where is the problem: maybe because I have dates arrays elements defined as strings at first and using moment.js I have them now as dates?
complete code:
var days=[];
var number=[1,2,3,4];
var departure=[09/06/2023,28/05/2023, 17/06/2023, 29/06/2023];
var rback=[15/06/2023,02/06/2023,18/06/2023,04/07/2023];
function enumerateDaysBetweenDates(startDate, endDate) {
startDate = moment(startDate,"DD/MM/YYYY");
endDate = moment(endDate,"DD/MM/YYYY");
var now = startDate, dates = [];
while (now.isBefore(endDate) || now.isSame(endDate)) {
dates.push(now.format("DD/MM/YYYY"));
now.add(1, 'days');
}
return dates;
};
for (i = 0; i < number.length; i++) {
var mdepart=departure[i];
var mrback=rback[i];
days.push(enumerateDaysBetweenDates(mdepart,mrback));
};
//Now I need to filter all dates that are not in June:
function checkd(num) {
return num.includes("/06/");
};
var june=days.filter(checkd);
//Error days.filter or empty array....
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.24.0/moment.min.js"></script>