从数组中删除特定日期

时间:2019-01-21 16:13:54

标签: javascript

我得到一个数组,其中包含指定范围内的日期(格式为日期,而不是字符串):

var dates = function(startDate, endDate) {
  var dates = [],
      currentDate = startDate,
      addDays = function(days) {
        var date = new Date(this.valueOf());
        date.setDate(date.getDate() + days);
        return date;
      };
  while (currentDate <= endDate) {
    dates.push(currentDate);
    currentDate = addDays.call(currentDate, 1);
  }
  return dates;
};

var startDate = new Date("2019-04-01");
var endDate = new Date("2019-04-26");

var dates = dates(startDate, endDate);

然后我像这样过滤掉周末:

var workingDays = dates.filter(function(e, index){  
  return (e.getDay() != 0 && e.getDay() != 6);
});

到目前为止,效果很好,但是我的问题是我还需要过滤假期。我尝试使用与周末相同的过滤器功能,如下所示:

var holidays = [new Date("2019-04-19"), new Date("2019-04-22")];

var workingDays = dates.filter(function(e, index){
  return (e.getDay() != 0 && e.getDay() != 6 && e != holidays);
});

那是行不通的,只是返回了与以前相同的数组。

有人知道我如何实现这一目标,而只是过滤掉变量中的指定日期?

3 个答案:

答案 0 :(得分:2)

var holidays = [new Date("2019-04-19").toString(), new Date("2019-04-22").toString()];
var dates = [new Date("2019-04-19"), new Date("2019-04-22"), new Date("2019-01-21")];

var workingDays = dates.filter(function(e, index){
  return (e.getDay() != 0 && e.getDay() != 6 && holidays.indexOf(e.toString()) === -1);
});

console.log(workingDays)

由于日期是对象,因此我们需要一些独特的属性,可以对其进行检查。在这种情况下,您可以尝试以下方法。但是我敢肯定还有一些更优化,更优雅的解决方案

答案 1 :(得分:1)

由于我们无法比较两个Date对象,因此我们可以比较它们的 ms 时间戳对应对象,例如:

!holidays.some(d => +d === +e)

其中+d+enew Date().getTime()的简写

示例:

var dates = function(startDate, endDate) {
  var dates = [],
      currentDate = startDate,
      addDays = function(days) {
        var date = new Date(this.valueOf());
        date.setDate(date.getDate() + days);
        return date;
      };
  while (currentDate <= endDate) {
    dates.push(currentDate);
    currentDate = addDays.call(currentDate, 1);
  }
  return dates;
};

var startDate = new Date("2019-04-01");
var endDate = new Date("2019-04-26");
var dates = dates(startDate, endDate);
var holidays = [new Date("2019-04-19"), new Date("2019-04-22")];

var workingDays = dates.filter(function(e, index){
  return (e.getDay() != 0 && e.getDay() != 6 && !holidays.some(d => +d === +e));
});

console.log(workingDays)

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/some

答案 2 :(得分:1)

e != holidays更改为!holidays.some( x => +x=== +e )

说明:+x是呼叫x.getTime()的快捷方式,用于比较日期时间戳。

var dates = function(startDate, endDate) {
  var dates = [],
      currentDate = startDate,
      addDays = function(days) {
        var date = new Date(this.valueOf());
        date.setDate(date.getDate() + days);
        return date;
      };
  while (currentDate <= endDate) {
    dates.push(currentDate);
    currentDate = addDays.call(currentDate, 1);
  }
  return dates;
};

var startDate = new Date("2019-04-01");
var endDate = new Date("2019-04-26");

var dates = dates(startDate, endDate);


var holidays = [new Date("2019-04-19"), new Date("2019-04-22")];

var workingDays = dates.filter(function(e, index){
  return (e.getDay() != 0 && e.getDay() != 6 && !holidays.some(x=>+x===+e));
});

console.log(workingDays);