I'm trying to find the difference between two Date arrays and then separate all different dates into another array of dates for later use. Also there is a little more functionality I want it to do. There is a variable diffDates. It should have to be an array because it can hold index of more than one months. With the help of that diffDates I want to populate selectDiffDates.
Please review this code to make it better and possibly faster and any ideas to implement the above mentioned last functionality in this piece of code. Thanks. Link to fiddle.
Array.prototype.diff = function(a) {
return this.filter(function(i) {
return (a.indexOf(i) < 0);
});
}
var monthIndex = [0,1,2,3,4,5,6,7,8,9,10,11];
var dMarked=[], dFiltered=[], selectDiffDates = [];
dMarked=["Thu Jan 01 2015 00:00:00 GMT+0500 (Pakistan Standard Time)",
"Thu Feb 05 2015 00:00:00 GMT+0500 (Pakistan Standard Time)",
"Thu Mar 05 2015 00:00:00 GMT+0500 (Pakistan Standard Time)",
"Thu Apr 02 2015 00:00:00 GMT+0500 (Pakistan Standard Time)",
"Thu Jun 04 2015 00:00:00 GMT+0500 (Pakistan Standard Time)",
"Thu Jan 01 2015 00:00:00 GMT+0500 (Pakistan Standard Time)"];
dFiltered=["Thu Jan 08 2015 00:00:00 GMT+0500 (Pakistan Standard Time)",
"Thu Feb 12 2015 00:00:00 GMT+0500 (Pakistan Standard Time)",
"Thu Mar 12 2015 00:00:00 GMT+0500 (Pakistan Standard Time)",
"Thu Apr 09 2015 00:00:00 GMT+0500 (Pakistan Standard Time)",
"Thu May 07 2015 00:00:00 GMT+0500 (Pakistan Standard Time)",
"Thu Jun 11 2015 00:00:00 GMT+0500 (Pakistan Standard Time)"];
var dMarkedMonths = [], dFilteredMonths = [];
for(var i=0; i<dMarked.length; i++){
dMarkedMonths.push( monthIndex[new Date(dMarked[i]).getMonth()]);
}
for(var i=0; i<dFiltered.length; i++){
dFilteredMonths.push( monthIndex[new Date(dFiltered[i]).getMonth()]);
}
console.log(dMarkedMonths);
console.log(dFilteredMonths);
var diffDates = dFilteredMonths.diff( dMarkedMonths );
console.log("Difference: "+diffDates);
for(var d=0; d<dFiltered.length; d++){
if(new Date(dFiltered[d]).getMonth() == diffDates){
selectDiffDates.push(dFiltered[d]);
}
}
console.log(selectDiffDates);
$("#console").html(selectDiffDates);