使用 moment.js 和 Pikaday 的无效日期

Invalid Date using moment.js and Pikaday

我正在尝试使用 Pikaday 将用户选择的日期传递到一个变量中,以便使用以下 javascript 在表单中进行处理,但我的页面返回 "Invalid Date"。

<script src="moment.js"></script>
<script src="pikaday.js"></script>

<script>
var picker = new Pikaday({
  field: document.getElementById('datepicker'), 
  firstDay: 1,
  minDate: moment().add({days: 20}).toDate(),
  disableDayFn: function(date){// Disable Monday 
    return date.getDay() === 0 || date.getDay() === 6;
  },
  onSelect: function(date) {
    field.value = moment(picker.toString()).format("MM/DD/YY");
  }
});

var selecteddate =  moment(picker.toString()).format("MM/DD/YY");
</script>

谁能看出我做错了什么?

您输入一个新选项 "format" 并更改 "onSelect":

示例

<html>
<head>
    <script type="text/javascript" charset="utf-8" src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.10.3/moment.js"></script>
    <script type="text/javascript" charset="utf-8" src="https://cdnjs.cloudflare.com/ajax/libs/pikaday/1.5.1/pikaday.js"></script>
</head>
<body>


    <div>

        <input type="text" id="datepicker">
<input type="text" id="datepicker2">

    </div>

<script>
var picker = new Pikaday({
  field: document.getElementById('datepicker'), 
  firstDay: 1,
  format:'MM/DD/YY',
  minDate: moment().add({days: 20}).toDate(),
  disableDayFn: function(date){// Disable Monday 
    return date.getDay() === 0 || date.getDay() === 6;
  },
  onSelect: function(date) {
   this._o.field.value =this.getMoment().format("MM/DD/YY");
  document.getElementById('datepicker2').value = picker.toString("MM/DD/YY");
  }
});

var selecteddate =  moment(picker.toString()).format("MM/DD/YY");
</script>
</body>

</html>