Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JQuery UI Datepicker date format issue with initial value

I need to set the initial date for the date picker to 03/20/2010 in mm-dd-yyyyy format. I have done this

<input id='datepicker' type='text' value='20/03/2010' />

But my problem is on clicking the field date picker populates with today's date as highlighed and no date selected, and up on selecting date value

But when i change my input field like one below

<input id='datepicker' type='text' value='03/20/2010' />

Date picker is populating March 20 as selected date and current date highlighted. But all in 'mm-dd-yyyy' format!

I want show all dates in 'dd-mm-yyyy' format. How can I solve this?

I tried some of the options and combinations below, but none of them solved my problem

$("#datepicker").datepicker();
$("#datepicker").datepick({dateFormat: 'mm/dd/yyyy'});
$("#datepicker").formatDate('dd/mm/yy');
$('#datepicker').datepicker("setDate", new Date($("#datepicker").val()) );

Update: Unfortunately i cant set Date value march 20 directly via javascript sice it is dynamically changing and is stored in a PHP variable. I need to get the default value from input field itself.

like image 835
Mithun Sreedharan Avatar asked Mar 11 '10 18:03

Mithun Sreedharan


2 Answers

$('#datepicker').datepicker('option', 'dateFormat', 'dd-mm-yy');

Year is specified as y for 2 digits and yy for 4 digits, as stated here.

like image 76
Alex Bagnolini Avatar answered Sep 19 '22 14:09

Alex Bagnolini


You need a combination of dateFormat and defaultDate options:

$("#datepicker").datepicker(
{
    dateFormat: 'mm/dd/yyyy',
    defaultDate: '20/03/2010'
});

there are, however, many different ways to specify the default date. I'm not sure what you need so you can reference the API here: http://docs.jquery.com/UI/Datepicker#option-defaultDate.

like image 39
Matt Ball Avatar answered Sep 20 '22 14:09

Matt Ball