简体   繁体   English

在JavaScript中找到两个日期数组之间的差异

[英]Finding difference between two arrays of Dates in JavaScript

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. 我试图找到两个Date数组之间的差异,然后将所有不同的日期分离为另一个日期数组,以供以后使用。 Also there is a little more functionality I want it to do. 此外,还有其他一些我想执行的功能。 There is a variable diffDates . 有一个变量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 . diffDates的帮助下,我想填充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);

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: diffDates是一个数组,因此您不应该将数组与一个月进行比较...而是应该检查diffDates数组中是否存在diffDates

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/ 在性能方面:请检查更新的小提琴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. 满足您需求的更好方法应该包括找到两个日期之间的差,而不是两个月。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM