bootstrap-datepicker event onchange not update enddatepicker startDate: value in second time

297 Views Asked by At
"use strict";
let nowDate = new Date();
let today = new Date(nowDate.getFullYear(), nowDate.getMonth(), nowDate.getDate(), 0, 0, 0, 0);
let enddate;

 $('#startdatepicker').datepicker({
      autoclose:true,
      todayHighlight: true,
      startDate:today
    }).on("changeDate",function(e){

        let present=new Date(e.date);
        enddate = new Date(present.getFullYear(), present.getMonth(), present.getDate(), 0, 0, 0, 0);


        $('#enddatepicker').datepicker({
         autoclose: true,
         startDate: enddate
        });

    });
1

There are 1 best solutions below

0
On BEST ANSWER

change onchange to 'change' , and your date not coming from the event variable , you need to get him 'manually' like this :

    "use strict";
let nowDate = new Date();
let today = new Date(nowDate.getFullYear(), nowDate.getMonth(), nowDate.getDate(), 0, 0, 0, 0);
let enddate;

 $('#startdatepicker').datepicker({
      autoclose:true,
      todayHighlight: true,
      startDate:today
    }).on("change",function(e){

    var fromDate = $('#startdatepicker').val().split("/");
    let present=new Date(fromDate[2] , fromDate[1] - 1 , fromDate[0]);
    enddate = new Date(present.getFullYear(), present.getMonth(), present.getDate() + 1, 0, 0, 0, 0);
    alert(enddate);

        $('#enddatepicker').datepicker('setStartDate', enddate );
        });