Calculate an expected delivery date (accounting for holidays) in business days using JavaScript?

后端 未结 8 1575
悲&欢浪女
悲&欢浪女 2020-12-16 05:04

After revisiting this script, and some modifications, the following is available to allow a user to add a feature that calculates the expected delivery date.



        
8条回答
  •  轮回少年
    2020-12-16 05:24

    I've adapted Mark Giblin's revised code to better deal with end of year dates and also U.S. federal holidays. See below...

    function businessDaysFromDate(date,businessDays) {
      var counter = 0, tmp = new Date(date);
      while( businessDays>=0 ) {
        tmp.setTime( date.getTime() + counter * 86400000 );
        if(isBusinessDay (tmp)) {
          --businessDays;
        }
        ++counter;
      }
      return tmp;
    }
    
    function isBusinessDay (date) {
      var dayOfWeek = date.getDay();
      if(dayOfWeek === 0 || dayOfWeek === 6) {
        // Weekend
        return false;
      }
    
      holidays = [
        '12/31+5', // New Year's Day on a saturday celebrated on previous friday
        '1/1',     // New Year's Day
        '1/2+1',   // New Year's Day on a sunday celebrated on next monday
        '1-3/1',   // Birthday of Martin Luther King, third Monday in January
        '2-3/1',   // Washington's Birthday, third Monday in February
        '5~1/1',   // Memorial Day, last Monday in May
        '7/3+5',   // Independence Day
        '7/4',     // Independence Day
        '7/5+1',   // Independence Day
        '9-1/1',   // Labor Day, first Monday in September
        '10-2/1',  // Columbus Day, second Monday in October
        '11/10+5', // Veterans Day
        '11/11',   // Veterans Day
        '11/12+1', // Veterans Day
        '11-4/4',  // Thanksgiving Day, fourth Thursday in November
        '12/24+5', // Christmas Day
        '12/25',   // Christmas Day
        '12/26+1',  // Christmas Day
      ];
    
      var dayOfMonth = date.getDate(),
      month = date.getMonth() + 1,
      monthDay = month + '/' + dayOfMonth;
    
      if(holidays.indexOf(monthDay)>-1){
        return false;
      }
    
      var monthDayDay = monthDay + '+' + dayOfWeek;
      if(holidays.indexOf(monthDayDay)>-1){
        return false;
      }
    
      var weekOfMonth = Math.floor((dayOfMonth - 1) / 7) + 1,
          monthWeekDay = month + '-' + weekOfMonth + '/' + dayOfWeek;
      if(holidays.indexOf(monthWeekDay)>-1){
        return false;
      }
    
      var lastDayOfMonth = new Date(date);
      lastDayOfMonth.setMonth(lastDayOfMonth.getMonth() + 1);
      lastDayOfMonth.setDate(0);
      var negWeekOfMonth = Math.floor((lastDayOfMonth.getDate() - dayOfMonth - 1) / 7) + 1,
          monthNegWeekDay = month + '~' + negWeekOfMonth + '/' + dayOfWeek;
      if(holidays.indexOf(monthNegWeekDay)>-1){
        return false;
      }
    
      return true;
    }
    

提交回复
热议问题