Time Lord minDate 无法解析

Tempus Dominus minDate counld not parse

第一次为 Bootstrap 4 使用 Tempus Dominus Datetimepicker

在我的代码中我有:

var dateFormat = 'DD-MM-YYYY';
var CurrDate   = '27-06-2018';
var MinDate    = '27-06-1918';
var MaxDate    = '27-06-2018';

然后我将日期转换为 moment 个对象:

dateMin = moment(CurrDate, dateFormat);
dateMin = moment(MinDate, dateFormat);
dateMax = moment(MaxDate, dateFormat);

我可以在 console.log() 中看到 moment 个对象,日期正确。

Moment {_isAMomentObject: true, _i: "27-06-2018", _f: "DD-MM-YYYY", _isUTC: false, _pf: {…}, …}
Moment {_isAMomentObject: true, _i: "27-06-1918", _f: "DD-MM-YYYY", _isUTC: false, _pf: {…}, …}
Moment {_isAMomentObject: true, _i: "27-06-2018", _f: "DD-MM-YYYY", _isUTC: false, _pf: {…}, …}

然后我像这样初始化 datetimepicker

// Initialize Stand Alone datetimepicker
$('#myDiv').datetimepicker({
    format: dateFormat,
    date: dateCurr,
    minDate: dateMin,
    maxDate: dateMax,
});

但它失败了:

minDate() Could not parse date parameter: NaN

为什么 datepickermoment 不满意? instructions here 说它应该有效。我错过/搞砸了什么吗?

您的代码似乎工作正常:

$(function() {
  var dateFormat = "DD-MM-YYYY";
  var CurrDate = "27-06-2018";
  var MinDate = "01-06-2018";
  var MaxDate = "27-06-2018";
  
  dateCurr = moment(CurrDate, dateFormat);
  dateMin = moment(MinDate, dateFormat);
  dateMax = moment(MaxDate, dateFormat);
  
  $("#datetimepicker1").datetimepicker({
    format: dateFormat,
    date: dateCurr,
    minDate: dateMin,
    maxDate: dateMax,
  });
});
<link href="https://use.fontawesome.com/releases/v5.1.0/css/all.css" rel="stylesheet"/>
<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.1.1/css/bootstrap.css" rel="stylesheet"/>
<link href="https://cdnjs.cloudflare.com/ajax/libs/tempusdominus-bootstrap-4/5.0.0-alpha14/css/tempusdominus-bootstrap-4.min.css" rel="stylesheet"/>

<div class="container mt-3">
  <div class="row">
    <div class="col">
      <div class="form-group">
        <div class="input-group date" id="datetimepicker1" data-target-input="nearest">
          <input type="text" class="form-control datetimepicker-input" data-target="#datetimepicker1" />
          <div class="input-group-append" data-target="#datetimepicker1" data-toggle="datetimepicker">
            <div class="input-group-text"><i class="fa fa-calendar"></i></div>
          </div>
        </div>
      </div>
    </div>
  </div>
</div>

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.22.2/moment.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/tempusdominus-bootstrap-4/5.0.0-alpha14/js/tempusdominus-bootstrap-4.min.js"></script>

检查您是否包含:

  • jQuery
  • moment.js
  • 时节-bootstrap-4.js