jQuery Datepicker: Need to add days to selected date

Posted On: September 3, 2017

If you are using the Datepicker jQuery plugin to allow customers to select dates according to week of the day, month, holiday.

The first date can be any date, but if holiday date will available than will not selectable as well as if use click on date than it will increase by dynamically assign date.

Have a look of below example:

natDays = [[1, 1], [12, 25]];
daysansmonths = '';

$("#order_bytraffic").change(function(){
					
						var varidate = new Date();
						
						// week day end  
						$("#dpick").unbind().datepicker({}).datepicker('setDate', varidate);	
						//console.log(daycrDa);
					 	$("#dpick").datepicker("option", "minDate", varidate);
						
						//testing date increment
						/*  aditnal3 = addDays( varidate, 0, daysansmonths); // got wednesday
						$("#dpick").unbind().datepicker({}).datepicker('setDate', aditnal3);
						$("#dpick").datepicker("option", "minDate", aditnal3);
						varidate = $('#dpick').datepicker('getDate');  */
						
						var daycrDaweekcond = varidate.getDay(); // tuesday 2 + 1days
						//console.log(varidate);
						var actualdaysinc = 3;
						var aditnal = '';
						// week day
						 if(daycrDaweekcond == 5){ // friday
							
							actualdaysinc = 3;
						    aditnal = addDays( varidate, actualdaysinc, daysansmonths); // got tuesday
						
							
						}else if(daycrDaweekcond == 6){ // saturday
							
							actualdaysinc = 3;
						    aditnal = addDays( varidate, actualdaysinc, daysansmonths); // got tuesday
						
							
						}else if(daycrDaweekcond == 0){ // sunday
							
							actualdaysinc = 2;
						    aditnal = addDays( varidate, actualdaysinc, daysansmonths); // got tuesday
						
							
						}
						// week day end
						//console.log(aditnal);
						
					
                      
						$("#dpick").unbind().datepicker({}).datepicker('setDate', aditnal);
						$("#dpick").datepicker("option", "minDate", aditnal);
						
						$("#dpick").datepicker("option", "beforeShowDay", unavailable);
						
						//common holiday strt
					 	var varidate = $('#dpick').datepicker('getDate');
					    
						var daycrDates = varidate.getDate(); // datenow
						var daycrgetMonth = varidate.getMonth() + 1; // month now getMonth()
						var combinedates = daycrDates + '/' + daycrgetMonth;

						var cgt = incnotabl(combinedates);
						var newinc='';
						while(cgt == 0){
							newinc = addDaysplusone(varidate, 1, daysansmonths);
							
							var daycrDates_newinc = newinc.getDate(); // datenow
						    var daycrgetMonth_newinc = newinc.getMonth() + 1; // month now getMonth()
						    var combinedates_newinc = daycrDates_newinc + '/' + daycrgetMonth_newinc;
							cgt = incnotabl(combinedates_newinc);
						} 
						//console.log(newinc);
						//console.log(varidate);
						
						 if(newinc !=''){
						 $("#dpick").unbind().datepicker({}).datepicker('setDate', newinc);
						 varidate = $('#dpick').datepicker('getDate');
						} 
						// week day
					 	var daycrDa = varidate.getDay(); // sun 0 + 2days 
						
						//zero day
						 var actualdaysinc_2days = 0;
						aditnal = addDays( varidate, actualdaysinc_2days, daysansmonths); // got monday 
							// console.log(aditnal);
						
						// week day end
						 $("#dpick").unbind().datepicker({}).datepicker('setDate', aditnal);
						
						//console.log(daycrDa);
					 	$("#dpick").datepicker("option", "minDate", aditnal);
						 
						 //holiday end
						
						$("#dpick").datepicker("option", "onSelect", onSelectCheckDate);
						
					
					 
				});
					
	
});
function onSelectCheckDate(date){
	
	var order_bytraffic_ch = $("#order_bytraffic").val();
	
	if(order_bytraffic_ch == 1){
	
	              var a = date.split("/") ;
	              var minValue = new Date(a[2], (a[1] - 1), a[0]);
				  var daycrDaweekcond = minValue.getDay();
				  //console.log(daycrDaweekcond);
				  //var varidate = $('#dpick').datepicker('getDate');
				  var actualdaysinc ='';
				  var aditnal ='';
				  
				  if(daycrDaweekcond == 5){
					 
					 actualdaysinc = 3;
					 aditnal = addDays( minValue, actualdaysinc, daysansmonths); // got tuesday
					 $("#dpick").datepicker('setDate', aditnal);
					 
				  }else if(daycrDaweekcond == 6){ // saturday
							
							actualdaysinc = 3;
						    aditnal = addDays( minValue, actualdaysinc, daysansmonths); // got tuesday
							$("#dpick").datepicker('setDate', aditnal);
						
							
						}else if(daycrDaweekcond == 0){ // sunday
							
							actualdaysinc = 2;
						    aditnal = addDays( minValue, actualdaysinc, daysansmonths); // got tuesday
							$("#dpick").datepicker('setDate', aditnal);
						
							
						}
						
						var daycrDates = minValue.getDate(); // datenow
						var daycrgetMonth = minValue.getMonth() + 1; // month now getMonth()
						var combinedates = daycrDates + '/' + daycrgetMonth;
						
						var cgt = incnotabl(combinedates);
						var newinc='';
						while(cgt == 0){
							newinc = addDaysplusone(minValue, 1, daysansmonths);
							
							var daycrDates_newinc = newinc.getDate(); // datenow
						    var daycrgetMonth_newinc = newinc.getMonth() + 1; // month now getMonth()
						    var combinedates_newinc = daycrDates_newinc + '/' + daycrgetMonth_newinc;
							cgt = incnotabl(combinedates_newinc);
						} 
					
						 if(newinc !=''){
						  actualdaysinc = 1;
							$("#dpick").datepicker('setDate', newinc);
						}
						
						
				
               				
	}  
				  
// selrection end	
}

function unavailable(date) {
	
	
       // dmy = date.getDate() + "/" + (date.getMonth() + 1) + "/" + date.getFullYear();
	   dmy = date.getDate() + "/" + (date.getMonth() + 1);
        if ($.inArray(dmy, unavailableDates) == -1) {
			var day = date.getDay();
            if(day == 0){
				return [false, "", "Unavailable"];
			}else{
			  return [true, ""];	
			}
		
        } else {
            return [false, "", "Unavailable"];
        }
		
		
		
}

 //alert(daysansmonths);
function addDays(dateObj, numDays, daysansmonths) {
	var d = numDays; //dayincrease
	
   dateObj.setDate(dateObj.getDate() + numDays);
   
   return dateObj;
}

function incnotabl(cgt){
	

     if ($.inArray(cgt, unavailableDates) == -1) {
            return 1;
        } else {
            return 0;
        }

	
}

function addDaysplusone(dateObj, numDays, daysansmonths) {
	var d = numDays; //dayincrease
	
   dateObj.setDate(dateObj.getDate() + numDays);
   
   return dateObj;
}

 

Also Read :   Marina di Portofino

Related Post

X
Subscribe to our newsletter
Subscribe to our newsletter today to receive updates on the latest news and offers.
100% Privacy. No spam guaranteed
webe tutorial
Menu