Add day with selected date using jquery datepicker

I am trying to add a day for another date field with a date selected from the current field

     ,
 onSelect: function(date) {
     var date2 = $('.currDate').datepicker('getDate');
       date2.setDate(date2.getDate()+1); 
       $('.nextDt').datepicker('setDate', date2);
    }

However i get date2.setDate(date2.getDate()+1);

Message: Object doesn't support this property or method

How can I solve this problem?

+5
source share
3 answers

This is because it currDatecan be empty.

If currDateemtpy $('.currDate').datepicker('getDate')returns null, in this case it date2.setDate(date2.getDate()+1);may display an error

Update:

$(function() {
    $('#nxtDate').datepicker({
        dateFormat: "dd-M-yy", 
    });
    $("#currDate").datepicker({
        dateFormat: "dd-M-yy", 
        minDate:  0,
        onSelect: function(date){
            var date2 = $('#currDate').datepicker('getDate');
            date2.setDate(date2.getDate()+1);
            $('#nxtDate').datepicker('setDate', date2);
        }
    });
})
+8
source

setDateand getDateare functions supported by Date()js, and getDate from datepicker is like a string, so you need to convert or try it:

onSelect: function(date) {  
     if(date!=undefined){
         var dateObject=new Date(date);
         dateObject.setDate(dateObject.getDate()+1);                                 
         $('.nextDt').datepicker('setDate', dateObject);
      }
    }

Here is a demo alert and the following date

+2
source
echo date('d-m-Y', strtotime("+".$nod." days"));

nod =

-3

All Articles