首页 > 解决方案 > daterangepicker how to get value and push 1day to show date on input text

问题描述

I would like to select a date and insert it in the table.

For example, I select a date:

enter image description here

After selecting a date it is inserted in a row as shown:

enter image description here

This is the Javascript code:

$('input[name="allotment"]').on('apply.daterangepicker', function(ev, picker) {
  var allomentDate = picker.endDate.format('DD') - picker.startDate.format('DD');
  console.log(allomentDate);
  console.log(picker);
  /* for (i=1;i<=allomentDate;i++){
      var dd = start.format('DD').toString();
      console.log(dd);
      var dateAllot = dd+1;
      console.log(dateAllot);
      var addRow = "<tr><td><input type='text' name='allomentDate[dateAllot]' readonly value=''></td>" + "<tr><td>";
  }*/
});

However it's just getting started. Now, I still can not get value the startdate and enddate.

标签: javascriptjquerydate

解决方案


 $('input[name="allotment"]').daterangepicker({
                "drops": "down",
                "opens": "center",
            }, function (start, end, label) {
                console.log("A new date selection was made: " + start.format('YYYY-MM-DD') + ' to ' + end.format('YYYY-MM-DD'));
                var dd = parseInt(end.format('DD')) - parseInt(start.format('DD'));
                var startDate = new Date(start.format('YYYY-MM-DD'));
                var endDate = new Date(end.format('YYYY-MM-DD'));
                //console.log(startDate.getDate());
                //console.log(endDate.getDate());
                //console.log(allot);
                for (i=1;i<=dd;i++){
                    var allot = startDate.getDate()+1;
                    console.log(allot);
                    var addRow = "<tr><td><input type='text' name='allomentDate[i]' readonly value='startDate'></td>" +
                        "<tr><td>";
                }
            });

将 start.format('YYYY-MM-DD') 设置为日期并使用 get.Date + 1


推荐阅读