2016-11-17 101 views
1

我正在一天日期的數組從API的陣列丟失一天:查找日期的JavaScript

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的日期數組中找到缺失的一天的最佳方法?

這樣我以後就可以將它作爲禁用日期傳遞給日期選擇器。

+0

什麼(工作)算法,你嘗試這麼遠嗎? –

+0

什麼是'...'意思? – Mahi

+0

我正在考慮這個過程:使用兩個for循環 –

回答

1

檢查了這一點創建一個新的陣列missingDates []

迭代數組(從API)在:

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

+0

@alereisan讓我知道你的想法在這...這工作,如果日期不合適,也如果不止一個日期被跳過...請注意,如果連續跳過一天以上,那也將被處理... – kukkuz

1

使用for循環

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

你可以做一個方法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));