5

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, ''];
            }
input
  • 7,503
  • 25
  • 93
  • 150
  • possible duplicate of [add/subtract business days in Javascript](http://stackoverflow.com/questions/6499944/add-subtract-business-days-in-javascript) – Tim Rogers Mar 02 '12 at 12:51
  • Oddly similar to http://stackoverflow.com/questions/9536127/jquery-ui-adding-date-to-selected-date – j08691 Mar 02 '12 at 17:02
  • @jo8691, there's a difference. the questioner there is asking this: `second must allow only dates greater than '3 of april'.` while I want to add 30 business/working days to the selected date in the 1st textfield and display it in the 2nd textfield. – input Mar 02 '12 at 17:35
  • @j08691 not even close –  Apr 02 '13 at 19:24
  • Thank you very much for showing `noWeekends` `boolean` usage! –  Apr 02 '13 at 19:25

3 Answers3

5

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);
Community
  • 1
  • 1
input
  • 7,503
  • 25
  • 93
  • 150
3
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
}
2

When dealing with holidays, there's really only one option: one day at a time. You should iterate, one day at a time, and add/substract as needed if a day "counts". In pseudo code:

date add(date startDate, int daysToAdd) {
    int i:=0
    endDate:=startDate
    while (i<daysToAdd) {
        endDate++
        if (    NOT(isWeekend(endDate))
            AND NOT(isHoliday(endDate)) {
            i++
        }
    }
    return endDate
}

isWeekend() is trivial to implement; isHoliday(), on the other hand, is a very tough nut to crack. The easiest way to deal with it is to have a table of known holidays and check if the date passed as parameter coincides with any of those. In my opinion, it's better to have a rule-based method that can compute whether a given date is a holiday or not.

Disclaimer: I copied and pasted this from a previous answer of mine to the same question. See How to ignore weekends and holidays in boost date time?

Community
  • 1
  • 1
Miguel Farah
  • 146
  • 1
  • 1
  • 7
  • Please see my updated question. How do I incorporate the function to add the business days? – input Mar 02 '12 at 17:43