繁体   English   中英

从日期数组中查找缺失的一天 javascript

[英]Find missing day from array of dates javascript

我从 API 中获取一组日期:

0:{date: "2016-11-17T00:00:00",…}
1:{date: "2016-11-18T00:00:00",…}
2:{date: "2016-11-19T00:00:00",…}
3:{date: "2016-11-21T00:00:00",…}
4:{date: "2016-11-22T00:00:00",…}
5:{date: "2016-11-23T00:00:00",…}

在此示例中,数组缺少此日期:

{date: "2016-11-20T00:00:00",…}

从 Javascript 或 Angular 中的日期数组中查找缺失日期的最佳方法是什么?

这样我以后就可以将它作为禁用日传递给日期选择器。

创建一个新数组missingDates []

使用for循环遍历数组(来自您的API)

for (i = 0; i < array.length; i++){
    var date1 = convert your array item (with index i) to a date
    var date2 = convert your array item (with index i + 1) to a date (keep in mind, index i + 1 cant be > than array.length)

    //calculate diffDays between the 2 dates, if diff is > 1, you have a missing date
    var missingDate = create your missing date (use your date1 variable + 1Day)

    //add misingDate to missingDates[] array
    missingDates.push(missingDate)
}

看一下这个:

  1. 首先,您可以使用Array.prototype.sort对数组进行排序(如果不是这样)

  2. 然后使用Array.prototype.reducehash table来查找缺少的日期

下面的代码段中给出的演示:

 var array=[ {date:"2016-11-17T00:00:00"}, {date:"2016-11-19T00:00:00"}, {date:"2016-11-18T00:00:00"}, {date:"2016-11-21T00:00:00"}, {date:"2016-11-22T00:00:00"}, {date:"2016-11-23T00:00:00"}, {date:"2016-11-27T00:00:00"} ]; var result = array.sort(function(a,b){ return Date.parse(a.date) - Date.parse(b.date); }).reduce(function(hash){ return function(p,c){ var missingDaysNo = (Date.parse(c.date) - hash.prev) / (1000 * 3600 * 24); if(hash.prev && missingDaysNo > 1) { for(var i=1;i<missingDaysNo;i++) p.push(new Date(hash.prev+i*(1000 * 3600 * 24))); } hash.prev = Date.parse(c.date); return p; }; }(Object.create(null)),[]); console.log(result); 
 .as-console-wrapper{top:0;max-height:100%!important;} 

如果没有缺少日期,您可以执行方法getMissingDate返回null如果大于一天的日期之间存在差异,则可以返回Date对象:

 var arr1 = [{date: "2016-11-17T00:00:00"}, {date: "2016-11-18T00:00:00"}, {date: "2016-11-19T00:00:00"}, {date: "2016-11-21T00:00:00"}, {date: "2016-11-22T00:00:00"}, {date: "2016-11-23T00:00:00"}], arr2 = [{date: "2016-11-17T00:00:00"}, {date: "2016-11-18T00:00:00"}, {date: "2016-11-19T00:00:00"}, {date: "2016-11-20T00:00:00"}, {date: "2016-11-21T00:00:00"}, {date: "2016-11-22T00:00:00"}, {date: "2016-11-23T00:00:00"}], getMissingDate = function(arr) { var result = null; for (var i = 0, l = arr.length - 1; i < l; i++) { var current = new Date(arr[i].date), next = new Date(arr[i + 1].date); if (1 < Math.ceil(Math.abs(next.getTime() - current.getTime()) / (1000 * 3600 * 24))) { result = new Date(current.setDate(current.getDate() + 1)); break; } } return result; }; console.log('arr1:', getMissingDate(arr1)); console.log('arr2:', getMissingDate(arr2)); 

 var array=[ {date:"2016-01-01T00:00:00"}, {date:"2016-03-01T00:00:00"}, {date:"2016-04-01T00:00:00"}, {date:"2016-07-01T00:00:00"}, {date:"2016-09-01T00:00:00"}, {date:"2016-11-01T00:00:00"}, {date:"2016-12-01T00:00:00"} ]; var result = array.sort(function(a,b){ return Date.parse(a.date) - Date.parse(b.date); }).reduce(function(hash){ return function(p,c){ var missingMonthsNo= (Date.parse(c.date) - hash.prev) / (1000 * 3600 * 24); if(hash.prev && missingMonthsNo> 1) { for(var i=1;i<missingMonthsNo;i++) p.push(new Date(hash.prev+i*(1000 * 3600 * 24))); } hash.prev = Date.parse(c.date); return p; }; }(Object.create(null)),[]); console.log(result);
 .as-console-wrapper{top:0;max-height:100%;important;}

暂无
暂无

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

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