Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bootstrap Datetimepicker set date

I am using a datetimepicker from Eonasdan which works fine so far.

I have a HTML element like this

<div id="datetimepicker"></div>

and use the datetimepicker function like this:

$("#datetimepicker").datetimepicker({
    inline: true,
    sideBySide: true,
    stepping: 5,
    locale: "de"
});

The result looks like this: enter image description here

I want to update the date via Javascript using the date function. My try looks like this:

$("#datetimepicker").data("DateTimePicker").date("2016-01-01");

Unfortunately this only returns a large object and does not alter the visible date in any way. This object is returned

Object { destroy: c/l.destroy(), toggle: c/ha(), show: c/ga(), hide: c/ba(),
    disable: c/l.disable(), enable: c/l.enable(), ignoreReadonly: c/l.ignoreReadonly(),
    options: c/l.options(), date: c/l.date(), format: c/l.format(), 40 weitere… }

Using a different time format or taking only date and no time or whatsoever does not help either.

None of the suggested solutions I found on various websites have solved my problem. I don't know whether I have to update the element? If so, how do I do it? I could not find any hint in the docs...

It is not an option to use the defaultDate since I need to do this process twice with two different dates.

like image 976
JRsz Avatar asked Nov 01 '16 19:11

JRsz


People also ask

What is bootstrap DateTimePicker?

Bootstrap 5 DateTimepicker. DateTimepicker is a form that adds the function of selecting date and time without the necessity of using custom JavaScript code.

Is there a bootstrap Datepicker?

Bootstrap date picker is a plugin that adds the function of selecting time without the necessity of using custom JavaScript code. This documentation may contain syntax introduced in the MDB 4.17.

How do I change the Datepicker format in bootstrap?

In order to set the date format, we only need to add format of one argument and then we will add our required format, which is shown in the following example: Example 1: In this example, we are going to use dd-mm-yyyy format.

What is a date time picker?

The DateTimePicker control is used to allow the user to select a date and time, and to display that date and time in the specified format. The DateTimePicker control makes it easy to work with dates and times because it handles a lot of the data validation automatically.


3 Answers

As per documentation you can use string, Date, moment, null, string and Date examples are below. Note default picker format.

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

or

$('#datetimepicker1').data("DateTimePicker").date('1/11/2016 12:23:12')
like image 50
Alexanderp Avatar answered Oct 03 '22 13:10

Alexanderp


I have debugged a little bit and I found this solution and it's working for me.

$('#datetimepicker').data("DateTimePicker").setValue('2020-05-3')

You will get all functions of datepicker by consoling this $('#datetimepicker').data("DateTimePicker")

like image 22
Umashankar Avatar answered Oct 03 '22 13:10

Umashankar


Sure, i'm real Necromancer, but i got same problem and .date() function does not help for me. But, i found this:

$('#datetimepicker1').data("DateTimePicker").options({ defaultDate: newdate});
// newdate - could be Date, Moment or ISO string
like image 24
ivan Avatar answered Oct 03 '22 14:10

ivan