2

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-06T19:00:00.000Z, 
Date 2015-01-13T19:00:00.000Z,Date 2015-01-20T19:00:00.000Z,Date 2015-01-27T19:00:00.000Z, 
Date 2015-02-03T19:00:00.000Z,Date 2015-02-10T19:00:00.000Z,Date 2015-02-17T19:00:00.000Z, 
Date 2015-02-24T19:00:00.000Z,Date 2015-03-03T19:00:00.000Z,Date 2015-03-10T19:00:00.000Z, 
Date 2015-03-17T19:00:00.000Z,Date 2015-03-24T19:00:00.000Z,Date 2015-03-31T19:00:00.000Z, 
Date 2015-04-07T19:00:00.000Z,Date 2015-04-14T19:00:00.000Z,Date 2015-04-21T19:00:00.000Z, 
Date 2015-04-28T19:00:00.000Z,Date 2015-05-05T19:00:00.000Z,Date 2015-05-12T19:00:00.000Z, 
Date 2015-05-19T19:00:00.000Z];
4
  • Can you provide some sample input and output? Commented Jan 7, 2015 at 9:40
  • What does selected and thisMonthDates look like before the loop? Commented Jan 7, 2015 at 9:43
  • @PeterAshwell I edited the question with the selected[] array. Commented Jan 7, 2015 at 9:48
  • @aduch I edited the question with the selected[] array. Commented Jan 7, 2015 at 9:48

2 Answers 2

1

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
    }
}
Sign up to request clarification or add additional context in comments.

Comments

0

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

And the next time when my loop iterates it again have to iterate through all those dates which creates issue. selected[] array should have to have less elements in it. As you can see there is another loop at the end which will be used to highlight the dates later on.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.