duanqiu2064 2014-02-18 15:53
浏览 246
已采纳

datepicker自定义日期更改格式

i have a working cusom button 'CSA' to display todays date + 6 months, but the date gets displayed as 2014-8-18. Now what i wish is 2014-08-18 and this for every month, 2 digits for every month.... working jsfiddle

    $(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');
                            $(input).datepicker("hide");
                            var date = new Date();
                            date.setMonth(date.getMonth() + 7);
                            $(input).val(date.getFullYear() + '-' +
                                date.getMonth() + '-' + date.getDate());

                        });


                    btn.appendTo(buttonPane);

                }, 1);
            }
        });

});
  • 写回答

2条回答 默认 最新

  • dtm41506 2014-02-18 15:57
    关注

    You can adjust your code to this:

    (date.getMonth()+7 < 10 ? '0'+date.getMonth()+7: date.getMonth()+7)

    I forgot the fact that we wrap around to the next year if it's the second half of the year. Also if it's more than 6 months say 20 months then that's an issue too. Here's where the mod operator(%) comes in handy. It gives you the remainder when dividing by the right hand side (for examples see documentation).

    Let's make a variable called monthsToAdd

    Then you can say

    var resultingMonth = (date.getMonth()+monthsToAdd)%12,
        displayedMonth = (resultingMonth < 10 ? '0'+ resultingMonth : resultingMonth);
    

    So the full code would be:

    $(function () {
      var resultingMonth = (date.getMonth()+monthsToAdd)%12,
          displayedMonth = (resultingMonth < 10 ? '0'+ resultingMonth : resultingMonth);
      $(".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');
                          $(input).datepicker("hide");
                          var date = new Date();
                          date.setMonth(displayedMonth);
                          $(input).val(date.getFullYear() + '-' +
                              date.getMonth() + '-' + date.getDate());
    
                      });
    
    
                  btn.appendTo(buttonPane);
    
              }, 1);
          }
      });
    });
    

    I just realized that this is in the wrong scope. So:

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

    Here's my fiddle

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 merge函数占用内存过大
  • ¥15 Revit2020下载问题
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 单片机无法进入HAL_TIM_PWM_PulseFinishedCallback回调函数
  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.