-->

bootstrap Datepicker : set value as last day of th

2019-09-04 04:35发布

问题:

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)

回答1:

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



回答2:

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