2

I got an array, which contains dates (formatted as dates, not strings) within a specified range:

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

Then I filtered out the weekends like this:

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

That worked perfectly so far, but my problem is that I need to filter out holidays aswell. I tried to use the same filter function as for the weekends, like this:

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

That didn't work tho and just returned the same array as before.

Does anyone know how I can achieve this and just filter out the specified dates in the variable?

1

3 Answers 3

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)

Since dates are objects we need some unique property we can check them on. In this case you could try the following. But I am sure there are some more optimized and elegant solutions

Sign up to request clarification or add additional context in comments.

Comments

1

Since we cannot compare two Date objects, we can compare instead their ms timestamp counterparts like:

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

where +d and +e is a shorthand for new Date().getTime()

Example:

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

1 Comment

Short and clean! Thanks alot, that's working perfectly.
1

Change e != holidays to !holidays.some( x => +x=== +e )

Explanation: +x is shortcut for call x.getTime() for compare dates timestamp.

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

1 Comment

Thanks alot! That's working, but Roko was first I guess. That's why I accepted his answer. +1 tho :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.