bootstrap Datepicker : set value as last day of th

2019-09-04 04:18发布

I have a bootstrap datepicker on months mode. I need the value to be last day of the month. How do I make this code work?

  $("#end-date").datepicker({
            minViewMode: 1,

    }).on('changeMonth',function(e)
        {                               
            //value of the end month is the last day of month       
            alert( new Date(e.date.getYear(), e.date.getMonth() + 1, 0));
              $("#end-date-catalog").datepicker('update', new Date(e.date.getYear(), e.date.getMonth() + 1, 0));
        });

plugin demo (here) jsfiddle (here)

2条回答
戒情不戒烟
2楼-- · 2019-09-04 04:44

I went into the js source code of the datePicker and I solved it by putting the trigger(changeMonth) after setting the value .

查看更多
啃猪蹄的小仙女
3楼-- · 2019-09-04 04:52

You don't have to change the source code for this to work. Just change the 'changeMonth' event to 'changeDate' event in your code. Also e.date.getYear() was returning 116 instead of 2016. That should be changed to e.date.getFullYear(). I will repost your code with my changes. Thanks a bunch because this helped me out!

$("#end-date").datepicker({
        minViewMode: 1,

}).on('changeDate',function(e)
    {                               
        //value of the end month is the last day of month       
        alert( new Date(e.date.getFullYear(), e.date.getMonth() + 1, 0));
          $("#end-date-catalog").datepicker('update', new Date(e.date.getFullYear(), e.date.getMonth() + 1, 0));
    });

For reference, here is a link to the docs, which is where I saw the difference between changeDate and changeMonth: https://bootstrap-datepicker.readthedocs.io/en/latest/events.html#changedate

查看更多
登录 后发表回答