日期选择器仅适用于两位数输入

时间:2018-09-20 07:02:46

标签: jquery html css date datepicker

在我的代码中,我有一个引导日期选择器,但是当我将日期设置为09/24/2018时它不起作用,但是9/24/2018正在运行。 我想在日期数字为个位数时添加一个0。

在Jquery中

          var onlyThisDates = ["09/25/2018", "09/26/2018", "09/29/2018", "09/27/2018", "09/20/2018", "09/21/2018", "09/22/2018", "10/01/2018", "10/03/2018", "10/05/2018", "10/11/2018", "10/15/2018", "10/17/2018"];

          console.log(onlyThisDates);

          $('#doctortime').datepicker({
              format: "mm/dd/yyyy",
              autoclose: true,
              beforeShowDay: function (date) {
                  var dt_ddmmyyyy =(date.getMonth() + 1)  + '/' + date.getDate() + '/' + date.getFullYear();
                  return (onlyThisDates.indexOf(dt_ddmmyyyy) != -1);
              }
          });

在HTML中

<input id="doctortime" class="form-control" placeholder="Doctor Date" name="dates" data-date-format="mm/dd/yyyy" /> 

1 个答案:

答案 0 :(得分:1)

你想要这样吗?

https://jsfiddle.net/9vqs2e1L/8/

    var onlyThisDates = ["09/20/2018", "09/26/2018", "09/29/2018", "09/27/2018", "09/20/2018", "09/21/2018", "09/22/2018", "10/01/2018", "10/03/2018", "10/05/2018", "10/11/2018", "10/15/2018", "10/17/2018"];         

      $('#doctortime').datepicker({
          format: "mm/dd/yyyy",
          autoclose: true,
           beforeShowDay: function (date) {                              
            var dt_ddmmyyyy = ('0' + (date.getMonth() + 1)).slice(-2)  + '/' + ('0' + date.getDate()).slice(-2) + '/' + date.getFullYear();                                     
            return (onlyThisDates.indexOf(dt_ddmmyyyy) != -1);                  
          }
      });