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 使用C#,asp.net读取Excel文件并保存到Oracle数据库
  • ¥15 C# datagridview 单元格显示进度及值
  • ¥15 thinkphp6配合social login单点登录问题
  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配