Search code examples
jquery-uidatepickermindate

Add few days to minDate in datepicker


How do I change this code

minDate: '2012/3/4'+'5D',

var dates = $( "#from, #to" ).datepicker({
    dateFormat: 'yy/mm/dd',
    changeMonth: true,

    **minDate: '2012/3/4'+'5D',**

    onSelect: function() { 
       var d1=new Date($('#from').val());
       var d2=new Date($('#to').val());
       $('#quantity').val((Math.ceil((d2-d1)/86400000)));
   }
}); 

to min date be=>2012/3/9

tanks.


Solution

  • Disclaimer: If it's actually hardcoded, just change it to '2012/3/9'.

    Parse the date, add 5 days, and convert it to a string again:

    var d = '2012/3/4';
    var parts = d.split('/');
    var date = new Date(+parts[0], +parts[1] - 1, +parts[2]);
    
    date.setDate(date.getDate() + 5);
    
    var result = date.getFullYear() + '/' + (date.getMonth() + 1) + '/' + date.getDate();
    

    Here's a demo.

    And here it is put in your code:

    var d = '2012/3/4';
    var parts = d.split('/');
    var date = new Date(+parts[0], +parts[1] - 1, +parts[2]);
    
    date.setDate(date.getDate() + 5);
    
    var result = date.getFullYear() + '/' + (date.getMonth() + 1) + '/' + date.getDate();
    
    var dates = $( "#from, #to" ).datepicker({
        dateFormat: 'yy/mm/dd',
        changeMonth: true,
        minDate: result,
        onSelect: function() { 
           var d1=new Date($('#from').val());
           var d2=new Date($('#to').val());
           $('#quantity').val((Math.ceil((d2-d1)/86400000)));
        }
    });