Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add days after date is selected excluding weekends and holidays

I have two text fields. In the first textfield I have added a calendar using jQueryUI Datepicker. When the user selects the date from the datepicker, it should automatically add 30 days to the date and set this value in the second textfield. I've got this working to add 30 days to the date.

However, it should exclude weekends and holidays and display only 30 business days. How do I do this?

if ($('#cat option:selected').text() == "ABC") {
    var date_billed = $('#datebilled').datepicker('getDate');
    var date_overdue = new Date();
    date_overdue.setDate(date_billed.getDate() + 30);
    date_overdue = $.datepicker.formatDate('mm/dd/yy', date_overdue);
    $('#datepd').val(date_overdue).prop('readonly', true);
}

Update: I have added the function noWeekendsOrHolidays to disable weekends or holidays. Is there anyway I can use this function to calculate and add business days to the date?

//holidays
            var natDays = [
              [7, 4, 'us']
            ];

            function noWeekendsOrHolidays(date) {
                var noWeekend = $.datepicker.noWeekends(date);
                if (noWeekend[0]) {
                    return nationalDays(date);
                } else { 
                    return noWeekend;
                }
            }
            function nationalDays(date) {
                for (i = 0; i < natDays.length; i++) {
                    if (date.getMonth() == natDays[i][0] - 1 && date.getDate() == natDays[i][1]) {
                        return [false, natDays[i][2] + '_day'];
                    }
                }
                return [true, ''];
            }
like image 362
input Avatar asked Mar 02 '12 12:03

input


2 Answers

Solved it! This answer here helped tremendously.

Code:

function AddBusinessDays(weekDaysToAdd) {
      var curdate = new Date();
      var realDaysToAdd = 0;
      while (weekDaysToAdd > 0){
        curdate.setDate(curdate.getDate()+1);
        realDaysToAdd++;
        //check if current day is business day
        if (noWeekendsOrHolidays(curdate)[0]) {
          weekDaysToAdd--;
        }
      }
      return realDaysToAdd;

    }


var date_billed = $('#datebilled').datepicker('getDate');
var date_overdue = new Date();
var weekDays = AddBusinessDays(30);
date_overdue.setDate(date_billed.getDate() + weekDays);
date_overdue = $.datepicker.formatDate('mm/dd/yy', date_overdue);
$('#datepd').val(date_overdue).prop('readonly', true);
like image 178
input Avatar answered Nov 15 '22 07:11

input


var numAdd = 30
var dataAvui = new Date()
for (var i=0;i<=numAdd;i++)
{ 
    var dataTemp = dataAvui
    console.dir(dataTemp.toString())
    dataTemp.setDate(dataTemp.getDate() + 1)
    if(dataTemp.getDay() == 6){
        dataTemp.setDate(dataTemp.getDate() + 2)
    }else if(dataTemp.getDay() == 0){
        dataTemp.setDate(dataTemp.getDate() + 1)
    }

    dataAvui = dataTemp
}
like image 27
user3140032 Avatar answered Nov 15 '22 08:11

user3140032