18

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/

0

11 Answers 11

52

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());
Sign up to request clarification or add additional context in comments.

2 Comments

Worth for me with eosnadan bootstrap 3 datepicker
They made the interesting decision to document the mentioned function on the "Options", instead of the "Functions" page of the manual.
21

The correct syntax is :

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

5 Comments

This does not provide an answer to the question. To critique or request clarification from an author, leave a comment below their post.
It totally answers the question. The author wanted to make his code work, because he couldn't figure out the right syntax. I answered with the correct syntax.
I apologise for the comment. It was autogenerated by review. It didn't show me the question and from just the answer, it looked like a cross question. Well, there are days SO reviews fail.
This answer only works if you haven't initialized the datetimepicker yet.
I'm gonna go all Matrix and claim that there is no question... Technically at least
18

Had to add, I was able to solve it.

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

1 Comment

Use "datetimepicker" if capitalised word is not working. All in simple letters works for me!
3

You want to set a default date value :

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

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

Comments

2

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 });

Comments

1

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

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/

Comments

1

Use defaultDate option to set date.

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

1 Comment

Uncaught TypeError: Cannot read properties of undefined (reading 'setValue')
0

None of the answers here will work in all situations. I've combined a couple of the answers here to get one that will:

var $ele = $("#datetimepicker2");
var date = new Date(1434544649384);
var datePickerObject = $ele.data("DateTimePicker");

if (typeof datePickerObject !== "undefined") {
  // it's already been Initialize . Just update the date.
  datePickerObject.date(date);
}
else {
  // it hasn't been initialized yet. Initialize it with the date.
  $ele.datetimepicker({
    date: date
  });
}

Comments

0

xfdai's answer worked for me, except if you're getting

$('#datetimepicker2').data("DateTimePicker")
= undefined

then try $('#datetimepicker2').data("datetimepicker") (lowercase data param) instead.

Comments

0

Here's what worked for me:

var dateString = '2200-12-31';
var dateParts = dateString.split('-');
var date = new Date(dateParts[0], dateParts[1] - 1, dateParts[2]);
$('#leaseEnd').data('datetimepicker').setDate(date);
$('#leaseEnd').data('datetimepicker').update();

Notes:

I had to use the lowercase term 'datetimepicker' - thanks Nathan Wilson

I had to make the string into a date object

I had to use the update() function to get the calendar to show the newly selected date

Don't forget that if you are including hours javascript may modify your update because of timezone difference. In that case include the timezone in your date string.

Comments

0
//date is my input datatimepicker class
$('.date').bootstrapMaterialDatePicker({
        format: 'DD/MM/YYYY',
        weekStart: 1,
        time: false,
        cancelText: 'Cancelar',
        okText: 'Elegir',
        nowText: 'Ahora',
        lang: 'es'
});
//then just use this
$('#idOfYourInput').val("30/11/2021");

1 Comment

Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.