`

js版 判断重大节日(国庆、春节)前后一周状态

    博客分类:
  • js
阅读更多
var holiday = {};

function inNationalDay(date) {
  var result = {
    beforeNationalDay: false,
    duringNationalDay: false
  };
  if (date) {
    var month = date.getMonth() + 1,
      day = date.getDate();
    if (month == 9 && (day >= 24 && day <= 30)) {
      result.beforeNationalDay = true;
    } else if(month == 10 && (day >= 1 && day <= 7)) {
      result.duringNationalDay = true;
    }
  }
  return result;
}

function inSpringFestival(date) {
  var result = {
    beforeSpringFestival: false,
    duringSpringFestival: false
  };
  if (date) {
    // set GMT+0800 hours(set china hour +8)
    date = new Date(date.getFullYear(), date.getMonth(), date.getDate(), 8);
    var time = date.getTime();
    var springFestivalDays = ['2015-02-19', '2016-02-08', '2017-01-28', '2018-02-16', '2019-02-05',
      '2020-01-25', '2021-02-12', '2022-02-01', '2023-01-22', '2024-02-10'];

    var springDay, springDate, beforeSpringDate, afterSpringDate;
    for (var i = 0; i < springFestivalDays.length; i++) {
      springDay = springFestivalDays[i];
      springDate = new Date(springDay);
      beforeSpringDate = new Date(springDay);
      beforeSpringDate.setDate(beforeSpringDate.getDate() - 7);
      afterSpringDate = new Date(springDay);
      afterSpringDate.setDate(afterSpringDate.getDate() + 7);
      if (time >= beforeSpringDate.getTime() && time < springDate.getTime()) {
        result.beforeSpringFestival = true;
        break;
      } else if (time >= springDate.getTime() && time < afterSpringDate.getTime()) {
        result.duringSpringFestival = true;
        break;
      }
    }
  }

  return result;
}

holiday.cache = {
  lastUpdateDate: null,
  beforeHoliday: false,
  duringHoliday: false
};

holiday.checkHoliday = function() {
  var now = new Date();
  if (!holiday.cache.lastUpdateDate) {
    holiday.cache.lastUpdateDate = now;
    var nationalDayResult = inNationalDay(now);
    var springFestivalResult = inSpringFestival(now);
    holiday.cache.beforeHoliday = nationalDayResult.beforeNationalDay || springFestivalResult.beforeSpringFestival;
    holiday.cache.duringHoliday = nationalDayResult.duringNationalDay || springFestivalResult.duringSpringFestival;
  } else {
    var lastUpdateDate = holiday.cache.lastUpdateDate;
    var cacheDate = lastUpdateDate.getFullYear() + '' + lastUpdateDate.getMonth() + '' + lastUpdateDate.getDate();
    var nowDate = now.getFullYear() + '' + now.getMonth() + '' + now.getDate();
    if (cacheDate != nowDate) {
      holiday.cache.lastUpdateDate = now;
      var nationalDayResult = inNationalDay(now);
      var springFestivalResult = inSpringFestival(now);
      holiday.cache.beforeHoliday = nationalDayResult.beforeNationalDay || springFestivalResult.beforeSpringFestival;
      holiday.cache.duringHoliday = nationalDayResult.duringNationalDay || springFestivalResult.duringSpringFestival;
    }
  }
};

module.exports = holiday;

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics