Set date to 7 working days from today (excluding weekends and public holidays)

后端 未结 3 1416
日久生厌
日久生厌 2021-01-24 08:30

I\'m trying to set a date to 7 working days from today\'s date (excluding weekends and UK public holidays).

  1. I start by setting the default date to today\'s date (t
相关标签:
3条回答
  • 2021-01-24 09:10

    If you only want to add 7 days and exclude holidays and weekends (assuming Saturday and Sunday), then you can just step from the start to the end and test each day as you go:

    var ukHolidays = ['2017-05-12','2017-05-29','2017-08-28','2017-12-25','2017-12-26'];
    
    // Return date string in format YYYY-MM-DD
    function getISODate(date){
      function z(n) {return (n<10?'0':'')+n}
      return date.getFullYear() + '-' + z(date.getMonth() + 1) + '-' + z(date.getDate());  
    }
    
    // Modifies date by adding 7 days, excluding sat, sun and UK holidays
    function add7WorkingDays(date) {
      for (var i=7; i; i--) {
        // Add a day
        date.setDate(date.getDate() + 1);
    
        // If a weekend or holiday, keep adding until not
        while(!(date.getDay()%6) || ukHolidays.indexOf(getISODate(date)) != -1) {
          date.setDate(date.getDate() + 1);
        }
      }
      return date;
    }
    
    // Add 7 working days to today
    var d = new Date();
    console.log('Add 7 working days to ' + d.toString() + 
                '\nResult: ' + add7WorkingDays(d).toString());

    0 讨论(0)
  • 2021-01-24 09:26

    Check one day at a time instead of a 7 day range.

    Start by setting the default date to today's date. Then, check one day at a time into the future. If that day is a working day, increment the workingDay counter by 1. if it's not, just loop onto the following day. When your workingDay counter hits 7, that's the date you need.

    0 讨论(0)
  • 2021-01-24 09:32

    Here is an example of what @andi is talking about. I made it as a calculator object.

    var calculator = {
        workDaysAdded: 0,
        ukHolidays: ['2017-05-12','2017-05-29','2017-08-28','2017-12-25','2017-12-26'],
        startDate: null,
        curDate: null,
    
        addWorkDay: function() {
            this.curDate.setDate(this.curDate.getDate() + 1);
            if(this.ukHolidays.indexOf(this.formatDate(this.curDate)) === -1 && this.curDate.getDay() !== 0 && this.curDate.getDay() !== 6) {
                this.workDaysAdded++;
            }
        },
    
        formatDate: function(date) {
            var day = date.getDate(),
                month = date.getMonth() + 1;
    
            month = month > 9 ? month : '0' + month;
            day = day > 9 ? day : '0' + day;
            return date.getFullYear() + '-' + month + '-' + day;
        },
    
        getNewWorkDay: function(daysToAdd) {
            this.startDate = new Date();
            this.curDate = new Date();
            this.workDaysAdded = 0;
            
            while(this.workDaysAdded < daysToAdd) {
                this.addWorkDay();
            }
            return this.curDate;
        }
    }
    
    var newWorkDay7 = calculator.getNewWorkDay(7);
    var newWorkDay9 = calculator.getNewWorkDay(9);
    var newWorkDay14 = calculator.getNewWorkDay(14);
    console.log(newWorkDay7);
    console.log(newWorkDay9);
    console.log(newWorkDay14);

    0 讨论(0)
提交回复
热议问题