天天看點

js日期比較,目前時間是否在兩個日期之間

var x = new Date('2015-05-25');
var y = new Date('2015-05-25');

// 1、比較兩個日期大小
if (x.getTime() == y.getTime()) {
  It'll be true;
}

// 2、比較兩個日期大小      
if (+x == +y) { It'll be true; } // 3、比較兩個日期大小 if (x - y == 0) { It'll be true; }      
// 是否在兩個日期之間

var beforeSpringDate, afterSpringDate;
  for (var i = 0; i < springFestivalDays.length; i++) {
    beforeSpringDate = new Date(springFestivalDays[i]);
    beforeSpringDate.setDate(beforeSpringDate.getDate() - 4);
    afterSpringDate = new Date(springFestivalDays[i]);
    afterSpringDate.setDate(afterSpringDate.getDate() + springFestivalPeriod - 1);
    if (time >= beforeSpringDate.getTime() && time <= afterSpringDate.getTime()) {
      result = true;
      break;
    }
  }