日期选择器自定义按钮错误

Datepicker custom button error?

本文关键字:错误 按钮 自定义 选择器 日期      更新时间:2023-09-26

我有这个脚本,其中有一个名为"csa"的额外按钮在日期选择器面板。现在一切都很好。例如,当有一个带有2014-01-23的输入字段,然后单击CSA按钮时,日期更改为+ 6个月。现在是第六个月。当我点击csa按钮时,我得到一个错误。日期2014-06-04变为2014-00-4。但应该是2014-06-04 + 6个月。但我看到没有错误在我的脚本....有人能帮我一下吗?

$(function () {
      $(".datepicker").datepicker({
          dateFormat: "yy-mm-dd",
          changeMonth: true,
          changeYear: true,
          yearRange: "2014:2034",
          showButtonPanel: true,
          beforeShow: function (input) {
              setTimeout(function () {
                  var buttonPane = $(input)
                      .datepicker("widget")
                      .find(".ui-datepicker-buttonpane");
                  var btn = $('<button class="ui-datepicker-current ui-state-default ui-priority-secondary ui-corner-all" type="button">CSA</button>');
                  btn.unbind("click")
                      .bind("click", function () {
                          //$.datepicker._clearDate(input);
                          //alert('custom text');
                          var date = new Date(),
                              monthsToAdd = 7,
                              resultMonth =((date.getMonth()+monthsToAdd)%12),
                              displayMonth = (resultMonth < 10 ? '0'+ resultMonth: resultMonth);
                              $(input).datepicker("hide");
                              $(input).val(date.getFullYear() + '-' +
                              displayMonth + '-' + date.getDate());
                      });

                  btn.appendTo(buttonPane);
              }, 1);
          }
      });
    });

Replace

resultMonth =((date.getMonth()+monthsToAdd)%12),

resultMonth =(((date.getMonth()+1)+monthsToAdd)%12),

原因:

date.getMonth()第六个月将返回5。月差为0-11

演示小提琴。