I am attempting to identify the variance between two sets of dates stored as arrays, and then segregate all distinct dates into a separate array for future use. Additionally, I have a specific functionality in mind. I have a variable called diffDates
. It needs to be an array as it may contain indices of multiple months. Using this diffDates
, I aim to populate selectDiffDates
.
I welcome any suggestions to enhance and potentially optimize this code, as well as any ideas on implementing the aforementioned additional functionality. Thank you. 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);