Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bootstrap 3 Datepicker v4 - set default date

im using this datetimepicker http://eonasdan.github.io/bootstrap-datetimepicker/ in my edit form. Im not able to set up default value in this datetimepicker from my variable date. If I use $('#edit_cost_date').val(json[0]['date']); on input element, the value in input is right but if i open datetimepicker i see that marked is not the date in input but actual date.

var date = json[0]['date'];
$(function () {
    $('#datetimepicker-edit-cost').datetimepicker({
        locale: 'cs',
        format:'DD.MM.YYYY'
    });
});
like image 314
porosman Avatar asked Mar 01 '15 15:03

porosman


People also ask

How do I change the default date format in bootstrap datepicker?

Go to line 1399 and find format: 'mm/dd/yyyy' . Now you can change the date format here.

How do I change the default date in datepicker?

Syntax: $(". selector"). datepicker( {defaultDate:"+6"} );

How do I change the date 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.


1 Answers

Are you sure your date variable is a Date object? If it is not, you will need to set the default date like this:

var date = json[0]['date'];
$(function () {
    $('#datetimepicker-edit-cost').datetimepicker({
        locale: 'cs',
        format:'DD.MM.YYYY',
        defaultDate: new Date(date)
    });
});

More information about the date property for that plugin:

http://eonasdan.github.io/bootstrap-datetimepicker/Functions/#date

date([newDate])

Takes newDate string, Date, moment, null parameter and sets the components model current moment to it. Passing a null value unsets the components model current moment. Parsing of the newDate parameter is made using moment library with the options.format and options.useStrict components configuration.

Throws

TypeError - in case the newDate cannot be parsed

Emits

change.dp - In case newDate is different from current moment

like image 110
John Washam Avatar answered Sep 24 '22 07:09

John Washam