Johan
Johan

Reputation: 211

Datepicker custom button error?

I have this script where there is an extra button called "csa" in the datepicker panel. Now everything worked fine. When there was for example an input field with 2014-01-23 and when then clicked on the CSA button, the date changed + 6 months. Now we are in the 6th month. I get an error when i click the csa button. the date 2014-06-04 becomes 2014-00-4. But it should be 2014-06-04 + 6 months. But i see no error in my script....can someone help me out here?

$(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);
          }
      });
    });

Upvotes: 0

Views: 63

Answers (1)

Shaunak D
Shaunak D

Reputation: 20646

Replace

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

with

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

Reason :

date.getMonth() will return 5 for sixth month. As monthrange is 0-11

Demo Fiddle.

Upvotes: 1

Related Questions