Reputation: 4539
First time using Tempus Dominus Datetimepicker
for Bootstrap 4
.
In my code I have:
var dateFormat = 'DD-MM-YYYY';
var CurrDate = '27-06-2018';
var MinDate = '27-06-1918';
var MaxDate = '27-06-2018';
I then convert the dates to moment
objects:
dateMin = moment(CurrDate, dateFormat);
dateMin = moment(MinDate, dateFormat);
dateMax = moment(MaxDate, dateFormat);
I can see the moment
objects in console.log()
, dates are correct.
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: {…}, …}
I then initialise the datetimepicker
like this:
// Initialize Stand Alone datetimepicker
$('#myDiv').datetimepicker({
format: dateFormat,
date: dateCurr,
minDate: dateMin,
maxDate: dateMax,
});
But it fails with:
minDate() Could not parse date parameter: NaN
Why is the datepicker
not happy with a moment
? The instructions here say it should work. Have I missed / messed up something?
Upvotes: 1
Views: 6597
Reputation: 5880
Your code seems to be working fine:
$(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>
Check that you included:
Upvotes: 2