I'm trying to separate each month's dates in each iteration. I have an array var selected=["pre populated with special dates"] which have all the selected dates. Now in this code how can I modify it to remove the dates for each month from selected[] array and populate thisMonthDates[] with only this particular month's dates in each iteration?
var ind=start.getMonth();
var thisMonthDates = [];
while(ind<=yearDifference){
for (var k = 0; k < selectedArrayLength; k++) {
if (new Date(selected[k]).getMonth() == monthIndex[ind]) {
thisMonthDates = selected[k];
//console.log(new Date(thisMonthDates[k]));
}
}
for(var eachDt=0; eachDt<thisMonthDates.length; eachDt++) {
//code for highlighting the dates
}
ind++;
}
Following is the selected[] array contents. And thisMonthDates[] is an empty array before the loop.
selected = [Date 2015年01月06日T19:00:00.000Z,
Date 2015年01月13日T19:00:00.000Z,Date 2015年01月20日T19:00:00.000Z,Date 2015年01月27日T19:00:00.000Z,
Date 2015年02月03日T19:00:00.000Z,Date 2015年02月10日T19:00:00.000Z,Date 2015年02月17日T19:00:00.000Z,
Date 2015年02月24日T19:00:00.000Z,Date 2015年03月03日T19:00:00.000Z,Date 2015年03月10日T19:00:00.000Z,
Date 2015年03月17日T19:00:00.000Z,Date 2015年03月24日T19:00:00.000Z,Date 2015年03月31日T19:00:00.000Z,
Date 2015年04月07日T19:00:00.000Z,Date 2015年04月14日T19:00:00.000Z,Date 2015年04月21日T19:00:00.000Z,
Date 2015年04月28日T19:00:00.000Z,Date 2015年05月05日T19:00:00.000Z,Date 2015年05月12日T19:00:00.000Z,
Date 2015年05月19日T19:00:00.000Z];
2 Answers 2
This loop should do the trick
for (var k = 0; k < selectedArrayLength; k++) {
if (new Date(selected[k]).getMonth() == monthIndex[ind]) {
thisMonthDates.push(selected.splice(k, 1));
k--; // since we removed an element we need to decrement k
}
}
Comments
See if this example helps:
http://plnkr.co/edit/XbnJV0B0bO00o4iJdBtV
var selected = [];
var JANUARY = 0
var FEBRUARY = 1
var MARCH = 2;
selected.push(new Date(2015, FEBRUARY, 15));
selected.push(new Date(2015, JANUARY, 10));
selected.push(new Date(2015, FEBRUARY, 6));
selected.push(new Date(2015, MARCH, 25));
var thisMonth = [];
for (var i = 0; i < selected.length; i++) {
if (selected[i].getMonth() === FEBRUARY) {
thisMonth.push(selected[i]);
}
}
console.log(selected);
console.log(thisMonth);
And the output:
[Sun Feb 15 2015 00:00:00 GMT+1100 (AEDT), Sat Jan 10 2015 00:00:00 GMT+1100 (AEDT), Fri Feb 06 2015 00:00:00 GMT+1100 (AEDT), Wed Mar 25 2015 00:00:00 GMT+1100 (AEDT)]
[Sun Feb 15 2015 00:00:00 GMT+1100 (AEDT), Fri Feb 06 2015 00:00:00 GMT+1100 (AEDT)]
1 Comment
Explore related questions
See similar questions with these tags.
selectedandthisMonthDateslook like before the loop?