NightMICU
NightMICU

Reputation: 9230

Bootstrap datetimepicker - date based on input value

I am using Eonasdan/bootstrap-datetimepicker and cannot figure out how to set the initial datetime when the page is loaded based on the input's value attribute.

When the page loads, the input is blank even though the field has a value.

<input type="text" name="start" id="start" value="2015-09-26 18:00"/>

JS -

$("#start").datetimepicker({
                ignoreReadonly: true,
                useCurrent: false,
                sideBySide: true,
                format: "MM/DD/YYYY HH:mm"
            });

Upvotes: 1

Views: 3633

Answers (1)

m.antkowicz
m.antkowicz

Reputation: 13571

your date format is not the same as declared in datetimepicker settings object so change the datetimepicker date format:

    //format: "MM/DD/YYYY HH:mm"
    format: "MM-DD-YYYY HH:mm"

you can also set the date using setDate() or setLocalDate()

    $("#start").data('datetimepicker').setDate(new Date(year, month, day, 00, 01));
    $("#start").data('datetimepicker').setLocalDate(new Date(year, month, day, 00, 01));

another option is to pass defaultDate when creating datetimepicker

    var dateNow = new Date();

    $("#start").datetimepicker({
            ignoreReadonly: true,
            useCurrent: false,
            sideBySide: true,
            format: "MM/DD/YYYY HH:mm",
            defaultDate:dateNow
        });

Upvotes: 3

Related Questions