0
function isInDate(MatchDate, entries) {
    for (c = 0; c < entries.length; c++) {
        var entry = entries[c];
        var DateFrom = new Date(entry.DateFrom);
        var DateTo = new Date(entry.DateTo);

        if (
            DateFrom.getMonth() <= MatchDate.getMonth() &&
            DateFrom.getDay() <= MatchDate.getDay() &&
            DateTo.getDay() >= MatchDate.getDay()
        ) {
            return true;
        }
    }

    return false;
}

I am passing in a "MatchDate" that I want to compare with every looped entries "DateFrom" and "DateTo", if the matchdate is in the timespan I want to return true, otherwise false.

It should also return true if its the same day as DateFrom & same day as DateTo... I can't get it to work whatever I try.. Could anyone please assist?

Joelgullander
  • 1,624
  • 2
  • 20
  • 46

2 Answers2

3

getDay returns the day of the week, not the day of the month.

You want to use getDate()

And you can simply just do

return MatchDate >= DateFrom && MatchDate <= DateTo;
epascarello
  • 204,599
  • 20
  • 195
  • 236
0

Should be like:

var pre = onload, isInDate; //for use on other loads
onload = function(){
if(pre)pre();
isInDate = function(MatchDate, entries){
  for(var c=0,l=entries.length; c<l; c++) {
    var entry = entries[c], DateFrom = new Date(entry.DateFrom), DateTo = new Date(entry.DateTo);
    var md = MatchDate.getTime();
    if(!(DateFrom.getTime() <= md && DateTo.getTime() >= md)){
      return false;
    }
  }
  return true;
}
}
StackSlave
  • 10,613
  • 2
  • 18
  • 35