Bootstrap Datetimepicker set date

穿精又带淫゛_ 提交于 2021-02-07 06:51:39

问题


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:

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.


回答1:


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')



回答2:


I think your HTML element should be an Input tag:

<input id="datetimepicker" type="text">

When the document ready:

$(document).ready(function () {
     $('#datetimepicker').datetimepicker({
            defaultDate: null,
            format: 'LT'
        });

})

You can set your date value via JavaScript.

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

or set null value, which is showing empty value.

$('#datetimepicker').data("DateTimePicker").date(null)

I tested it. It's working.




回答3:


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



回答4:


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")




回答5:


Ok for someone who is trying $('#datetimepicker').data("DateTimePicker").date(null) and you get error .data is undefined. Then you can use below jugaad, I do not know how good it is, but it does worked out for me

function getDateTimeObj(ele) {
    if(ele && ele[0]){
        for(k in ele[0]){
            if(ele[0][k].hasOwnProperty('DateTimePicker')){
                return ele[0][k]['DateTimePicker'];
            }
        }
    }
}

USAGE:

getDateTimeObj($('#datetimepicker')).date(null);


来源:https://stackoverflow.com/questions/40366878/bootstrap-datetimepicker-set-date

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!