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
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
}
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);
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?