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, ''];
}
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);
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
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With