匹配两个日期,如果匹配则返回true

时间:2017-09-21 23:43:52

标签: javascript

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

我正在通过&#34; MatchDate&#34;我希望与每个循环条目进行比较&#34; DateFrom&#34;和&#34; DateTo&#34;,如果匹配日期在时间范围内,我想返回true,否则为false。

如果它与DateFrom&amp;同一天也应该返回true。 DateTo当天...我无法尝试任何工作......有人可以帮忙吗?

2 个答案:

答案 0 :(得分:3)

getDay返回星期几,而不是月中的某天。

您想使用getDate()

你可以简单地做到

return MatchDate >= DateFrom && MatchDate <= DateTo;

答案 1 :(得分:0)

应该像:

&#13;
&#13;
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;
}
}
&#13;
&#13;
&#13;

相关问题