代码之家  ›  专栏  ›  技术社区  ›  Tobias Glaus

从数组中删除特定日期

  •  2
  • Tobias Glaus  · 技术社区  · 6 年前

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

    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 回复  |  直到 6 年前
        1
  •  1
  •   Roko C. Buljan    6 年前

    因为我们不能比较两个日期对象,所以我们可以比较它们 毫秒 时间戳对应项,如:

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

    哪里 +d +e new 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
  •  2
  •   Jelle    6 年前

    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)

    因为日期是对象,所以我们需要一些独特的属性来检查它们。在这种情况下,您可以尝试以下操作。但我相信有一些更优化和优雅的解决方案

        3
  •  1
  •   Kamil Kiełczewski    6 年前

    变化 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);