Set date in bootstrap datetimepicker input

Set date in bootstrap datetimepicker input

I used the datetimepicker from: http://eonasdan.github.io/bootstrap-datetimepicker/Installing/
There no provide how to use function (date, show, hide, etc) so I don’t know how to set the value for input using datetimepicker using date function which says:

date([newDate])
Takes string, Date, moment, null parameter and sets the components
model current moment to it.

I tried
$(‘#datetimepicker2’).datetimepicker(
date: function() { return new Date(1434544649384); }
);

Where 1434544649384 is a timestamp.
But it doesn’t work and doesn’t update input text/value…
JsFiddle: http://jsfiddle.net/0Ltv25o8/1397/

Solutions/Answers:

Solution 1:

The correct syntax is :

$('#datetimepicker2').datetimepicker({
    date: new Date(1434544882775)
});

Solution 2:

quote from http://eonasdan.github.io/bootstrap-datetimepicker/Functions/

Note All functions are accessed via the data attribute e.g. $('#datetimepicker').data("DateTimePicker").FUNCTION()

So you can update date like following:

$('#datetimepicker2').data("DateTimePicker").date(new Date());

Solution 3:

Had to add, I was able to solve it.

$('#datetimepicker1').datetimepicker({});
$('#datetimepicker1').data("DateTimePicker").date(moment(new Date ).format('DD/MM/YYYY HH:mm'));

Solution 4:

You want to set a default date value :

var d = $('#datetimepicker1').datetimepicker({
    "defaultDate":new Date()
});

http://jsfiddle.net/0Ltv25o8/1399/

Solution 5:

For me although I managed to change the date, it also changed the format incorrectly. When I applied the format separately it then worked as expected:

var d = $('#datetimepicker1').({ date: moment(), format: $scope.userDateFormatMoment });

Solution 6:

If you are looking to set the date of your datetimepicker, use the defaultDate option. From the documentation:

Related:  What is the unicode character for the close symbol used by Twitter bootstrap?

defaultDate: Sets the picker default date/time. Default: false. Accepts: date, moment, string.

Your options would look like:

$('#datetimepicker2').datetimepicker({
    defaultDate: new Date(1434544649384)
});

http://jsfiddle.net/0Ltv25o8/1400/

References