Connect with us

Blog

jQuery Datepicker: Need to add days to selected date

mm

Published

on

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 :   Datepicker Widget - jQuery UI API Example Of Code

Hi, Thank you for reading my article. I am Victor, a professional blogger from Jaipur, India.I started webetutorial for blogging & sharing solution for developer questions, and now it’s empowering globally by helping them to make money.I am writing about starting & managing a blogs, WordPress, Magento, Social Media, SEO, Marketing, making money online, Investment, Finance, Gadgets, Fitness and more.

Continue Reading
Advertisement
Click to comment

Leave a Reply

Your email address will not be published. Required fields are marked *

12 − ten =

Blog

Keys to Blogging Success: How to Write an Awesome Blog Post

mm

Published

on

Keys to Blogging Success: How to Write an Awesome Blog Post

In this article we will talk about how to write a quality blog post. This article will give you some idea to enhance your post quality and increase content word limit.

Follow the below points to write a blog:

Make A Plan:

Before starting on any blog we have to make a plan on which topic we are going to write and how many words we can write. We need to choose the words and headings of content on which readers is driving on search engine. Blog plan will maintain a well-created synopsis with a target goal to reach your readers.

Create A Good Title Of Blog:

You need to stand out in the today competition need a good title for your blog which define your whole blog in a single line. Your title should be completed in a 60 characters because greater title will escape by some search engine like Google.

Make sure your blogs all point is clear from the beginning which creates a connection between you and your readers not only for current blog they will connect for further blogs.

Also Read :   jQuery Menu Effect Plugins

Keep It Clean & Formatted:

Blog content should be proper aligned and well-formatted with an organized structure. Only need to describe the information which is important and will not confuse your reader.

First all blogs should write in the Microsoft word or open word which gives formatted structure to your blog and make low chance for wrong spell.  After ensuring that your post is good enough and everything looks fine to be published upload content on your live blogging website.

Write Down The Truth:

When you are going to write a any blog you need to write down the truth content because readers only love true websites. You also need to respect your readers and competitor bloggers to increase traffic on your website.

On-Page SEO:

Blog must have SEO friendly title, meta keywords, meta description, Image alt tags, Headings, Inbound and outbound links. On-page seo will give change to your blog to show on top of search result in search engines. For the WordPress blogs you can install and configuring Yoast SEO plugin.

Also Read :   Calendar Girls: It's nasty, sensational

Wrap Up:

Once you have completed your blog you should write a 2-3 line of short description which you have wrote because that will wrapping your blog overview and make your blog attractive.

For example: In this blog we talk about grows in traffic, revenue, authority topic and sharing personal experiences. The all of things describe here will increase your content quality for blogs.

To get more news on content writing, subscribe us or join us to advertise your blog with us.

Continue Reading

Subscribe to our newsletter

Enter your email address to subscribe to blog and receive notifications of new articles by email.
100% Privacy. No spam guaranteed

Trending