alex
alex

Reputation: 4924

JQuery datepicker - Why do I need to call it twice for it to work?

This is a bit of a general question, but I might be overlooking something. The following datepicker logic doesn't work:

$('#date_from').datepicker({
                            dateFormat: "yy-mm-dd",
                            setDate: new Date('<?= $_REQUEST['date_from'];?>')
                           });

However when I call the datepicker twice it does work, like:

$('#date_from').datepicker({
                            dateFormat: "yy-mm-dd"
                           });
$('#date_from').datepicker('setDate', new Date('<?= $_REQUEST['date_from'];?>'));

Personally, I would like to use the first one and to know why it doesn't work.

Upvotes: 0

Views: 93

Answers (1)

Nguyen Pham
Nguyen Pham

Reputation: 464

You can refer here http://api.jqueryui.com/datepicker/#option-dateFormat

First you need the first line to format the date to your desired format

$( ".selector" ).datepicker({
    dateFormat: "yy-mm-dd"
});

Then you can set/get the date in your field

// Getter
var dateFormat = $( ".selector" ).datepicker( "option", "dateFormat" );
// Setter
$( ".selector" ).datepicker( "option", "dateFormat", "yy-mm-dd" );

Upvotes: 1

Related Questions