javascript - How to set Min and Max Date in Jquery Date Selection depending on current date -


the jsfiddle can found here: http://jsfiddle.net/pqfdc/682/

i need to:
pick date in 365 day period based on user selection input. example user inputs 01/01/2016, can select date between 01/01/2016 , 01/01/2017 , nothing after that.

i have set method if date before 31st jan minimum start , end date can vary. example dates user can choose 31/01/2015 - 31/12/2016

var d = new date(); var month = d.getmonth();var month = month + 1; var year = d.getfullyear(); var day = d.getdate();  if(day <= 9){ var day = "0".concat(day);}    if(month <= 9){ var month = "0".concat(month);}    if(month == 01 && day <= 31){ var year = year - 1; var minyear = "31-01-"+year; var year = year + 1; var maxyear = "31-12-"+year; } else {  var minyear = "31-01-"+year; var year = year + 1; var maxyear = "31-12-"+year; }   $(function() {       /* global setting */     var datepickersopt = {         dateformat: 'dd-mm-yy',         mindate   : 0     }      $("#txtstrtdate").datepicker($.extend({         onselect: function() {             var mindate = $(this).datepicker('getdate');             mindate.setdate(mindate.getdate()-365); //add 2 days             $("#txtexpdte").datepicker( "option", "mindate", mindate);         }     },datepickersopt));      $("#txtexpdte").datepicker($.extend({         onselect: function() {             var maxdate = $(this).datepicker('getdate');             maxdate.setdate(maxdate.getdate()+365);             $("#txtstrtdate").datepicker( "option", "maxdate", maxdate);         }     },datepickersopt)); });  

based on jsfiddle example:

var datepickersopt = {     dateformat: 'dd-mm-yy',     mindate   : 0 }  $("#txtstrtdate").datepicker($.extend({     onselect: function() {         var mindate = $(this).datepicker('getdate');         var maxdate = new date();          $('#txtexpdte').datepicker('option', 'mindate', mindate);          // add 365 days selected date         maxdate.setdate(mindate.getdate() + 365);          $("#txtexpdte").datepicker( "option", "maxdate", maxdate);     } },datepickersopt));  $("#txtexpdte").datepicker($.extend({},datepickersopt)); 

link jsfiddle: http://jsfiddle.net/2op64ytl/1/

result:

enter image description here


Comments

Popular posts from this blog

authentication - Mongodb revoke acccess to connect test database -

r - Update two sets of radiobuttons reactively - shiny -

ios - Realm over CoreData should I use NSFetchedResultController or a Dictionary? -