如何在javascript中合并连续的日期?

时间:2018-07-19 10:16:40

标签: javascript arrays momentjs

我有一系列日期,例如:

test = [ '2018-07-18', '2018-07-19', '2018-07-21', '2018-07-23', '2018-07-24', '2018-07-26'];

我想返回一个连续日期的子数组数组,如下所示:

result = [['2018-07-18', '2018-07-19'], ['2018-07-21'], ['2018-07-23', '2018-07-24'], ['2018-07-26']]

我正在尝试编写代码段:

const moment = require('moment');
let visited = [];
const alpha = test.reduce((accumlator, current_date, current_index, array) => {
    let start_date = current_date;
    let successive_date = array[current_index + 1];
    visited.push(start_date);
    if(successive_date && moment(successive_date).diff(moment(start_date), 'days') === 1  
        && visited.includes(successive_date) === false) {
        accumlator.concat(start_date);
        accumlator.concat(successive_date);
    }
    if(successive_date && moment(successive_date).diff(moment(start_date), 'days') !== 1  
        && visited.includes(successive_date) === false) {
        accumlator.concat(successive_date);
    }
    return accumlator;
}, []);

console.log('alpha: ', alpha);

使用concat时的结果是:

alpha: []

我使用了push(),它返回了一个像test这样的数组:

alpha:  [ '2018-07-18','2018-07-19','2018-07-21','2018-07-23','2018-07-23','2018-07-24''2018-07-26' ]

如何解决此问题以获得如上所述的结果?

3 个答案:

答案 0 :(得分:4)

您可以尝试:

test.reduce((acc, date) => {
  const group = acc[acc.length - 1];
  if (moment(date).diff(moment(group[group.length - 1] || date), 'days') > 1) {
    acc.push([date])
  } else {
    group.push(date);
  }
  return acc;
}, [[]])

输出:

[
  [
    "2018-07-18",
    "2018-07-19"
  ],
  [
    "2018-07-21"
  ],
  [
    "2018-07-23",
    "2018-07-24"
  ],
  [
    "2018-07-26"
  ]
]

答案 1 :(得分:1)

如果不保留数组中日期的顺序,则以下帮助。例如,'2018-07-18', '2018-07-19', '2018-07-17'是连续的,但分散在数组的开头和结尾。

var test = [ '2018-07-18', '2018-07-19', '2018-07-21', '2018-07-23', '2018-07-24', '2018-07-26', '2018-07-17'], dateformat = "YYYY-MM-DD";

var result = test.reduce(function(acc,val){
    var present, date = moment(val,dateformat);
    acc.forEach(function(arr,index){
        if(present) return;
        if(arr.indexOf(date.clone().subtract(1,'day').format(dateformat))>-1 || arr.indexOf(date.clone().add(1,'day').format(dateformat))>-1)
        {
            present = true;
            arr.push(val);
        }
    });
    if(!present) acc.push([val]);
    return acc;
},[]);

console.log(result)
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.22.2/moment.min.js"></script>

答案 2 :(得分:0)

您可以使用此功能,但日期应该按顺序排列。

function get_relative_dates(dates){
    var format = 'YYYY-MM-DD';
    var newDate = [];
    dates.forEach(function(date){
        var lastArr, lastDate;
        if(newDate.length){
            lastArr = newDate[newDate.length -1];
            if(!lastArr.length)
                lastArr.push(date);
            else{
                var lastDate = lastArr[lastArr.length -1];
                if(moment(lastDate, format).add(1,'d').format(format) == date)
                    lastArr.push(date);
                else
                    newDate.push([date]);
            }
        }
        else
            newDate.push([date]);
    });
    return newDate;
}