我尝试使用以下代码将月份更改为月末更改,但日期不会更改。
这是我的aspx代码:
<div class="input-group date toDatePicker" id="toDate">
<asp:TextBox ID="txt_todate" runat="server" TabIndex="2" class="form-control toDatePicker" placeholder="Select Date"></asp:TextBox>
<span class="input-group-addon">
<span class="glyphicon glyphicon-calendar"></span>
</span>
</div>
和使用的js是:
$('.toDatePicker').datepicker
orientation: "bottom left",
minViewMode: 1,
maxViewMode: 4,
autoclose: true,
format: "mm/dd/yyyy",
}).on('changeMonth', function (value) {
var endDate = new Date(value.date);
var mm = endDate.getMonth() + 1; //January is 0!
var yyyy = endDate.getFullYear();
var dd = endDate.daysInMonth(mm, yyyy);
if (dd < 10) {
dd = '0' + dd;
}
if (mm < 10) {
mm = '0' + mm;
}
endDate = new Date(yyyy, mm, dd);
$('.toDatePicker').datepicker('setDate', endDate).datepicker('update');
});
function daysInMonth(month, year) {
return new Date(year, month, 0).getDate();
};
答案 0 :(得分:1)
您的问题是“changeMonth”事件以e.date返回,其中e.date是当前日期。如果changeMonth事件是由点击“下个月”箭头引起的,那么这是有道理的。如果是由于点击“月外日期”引起的,那么e.date仍然是前一天,而不是新日期。没有更多信息,我猜这是你的问题。
如果您想要新选择的日期,那么您需要一个'changeDate'事件,它将新日期作为e.date传递。
另见这一点,这表明行为可能是一个缺陷: https://github.com/uxsolutions/bootstrap-datepicker/issues/2290
文档: https://bootstrap-datepicker.readthedocs.io/en/stable/events.html#changemonth