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

时间:2016-09-14 10:35:52

标签: javascript datepicker momentjs pikaday

我尝试使用Pikaday将用户选择的日期传递到要使用以下javascript在表单中处理的变量,但我的页面返回"无效的日期"。

<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>

有谁能看到我在这里做错了什么?

1 个答案:

答案 0 :(得分:0)

你提出了一个新选项&#34;格式&#34;并改变&#34; onSelect&#34;:

实施例

&#13;
&#13;
<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>
&#13;
&#13;
&#13;