Fiddle: https://jsfiddle.net/ek9c9ojx/3/
I tried numerous solutions from this question: set default time in bootstrap-datetimepicker
But none of them worked. They all cause the input to be empty and default to the placeholder text.
I tried the setDate()
, setLocalDate()
, and defaultDate
routes but I can't find anything in the current documentation supporting these or any current methods that work to achieve this simple task.
None of these work:
var dateObj = new Date('2016-03-21T23:31:00');
// does not work, causes empty input/placeholder text
$("#startdatetime-from").data('datetimepicker').setDate(dateObj);
// does not work, causes empty input/placeholder text
$("#startdatetime-from").data('datetimepicker').setLocalDate(dateObj);
var dateObj = new Date('2016-03-21T23:31:00');
$('#new-challenge-date-time-picker').datetimepicker({
minDate: moment(),
defaultDate: dateObj // does not work, same symptoms as above
});
I also tried setting the value
attribute of the #new-challenge-date-time-picker
itself, but that did not work either.
Does anyone have the answer?
You can pass the date to "defaultDate" while initializing the picker. But minDate will override the defaultDate. So if you need minDate as well then set useCurrent to false
https://eonasdan.github.io/bootstrap-datetimepicker/Options/#mindate
https://eonasdan.github.io/bootstrap-datetimepicker/Options/#usecurrent
Try the below
$(function() {
var datePreset = $('#new-challenge-date-time-picker').attr('data-preset');
var dateParts = datePreset.split(' ');
var dateString = dateParts[0];
var dateTime = dateParts[1];
var dateTimeParts = dateTime.split(':');
var dateHour = dateTimeParts[0];
var dateMinutes = dateTimeParts[1];
var dateAMPM = dateParts[2];
var dateStringParts = dateString.split('/');
var dateMonth = dateStringParts[0];
var dateDay = dateStringParts[1];
var dateYear = dateStringParts[2];
var time = (dateAMPM == 'PM') ? (parseInt(dateHour) + 12) + ':' + dateMinutes : dateHour + ':' + dateMinutes;
time += ':00';
console.log(dateYear + '-' + dateMonth + '-' + dateDay + 'T' + time);
var dateObj = new Date(dateYear + '-' + dateMonth + '-' + dateDay + 'T' + time);
$('#new-challenge-date-time-picker').datetimepicker({
minDate: moment(),
defaultDate: dateObj,
useCurrent:false
});
// $("#new-challenge-date-time-picker").data('datetimepicker').setDate(dateObj);
});