2

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);

1 Answer 1

1

diffDates is an array, so you should not compare an array with single month...rather you should check if month exists in the diffDates array:

for(var d=0; d<dFiltered.length; d++){
    if(diffDates.indexOf(new Date(dFiltered[d]).getMonth())>-1){
     selectDiffDates.push(dFiltered[d]);   
    }
}

On the performance part: please check the updated fiddle http://jsfiddle.net/q7rmr5uq/3/

Your current logic does not follow your requirement. Currently you are picking months and finding different months and then making the dates again from the months you got. But this would skip all those dates who has same month but different day or year.

The better approach for your requirement should include finding the difference between two dates rather than two months.

Sign up to request clarification or add additional context in comments.

5 Comments

diffDates each element should be compared with every element of the dFiltered is this what above code is doing?
Yes, because diffDates is an array of months.
Any tips please for making this code work faster? As this going to become part of messy & cumbersome code :)
I got one tip from your profile Array direct assignment is even faster than Push I'm doing lots of push in my code. Thanks
Did you read the comment in that post??? You're actually benchmarking against IE8? Dear me. Push is significantly faster in both Chrome and Firefox latest and it's actually a draw in IE11. Your performance argument was probably valid 5 years ago

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.