14

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.

Nick Veys
  • 23,458
  • 4
  • 47
  • 64
Mithun Sreedharan
  • 49,883
  • 70
  • 181
  • 236
  • looking at your code i have changed my code to $("#datepicker").datepicker();$('#datepicker').datepicker("setDate", new Date($("#datepicker").val()) ); and it help me – AEMLoviji Nov 30 '12 at 06:48

5 Answers5

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

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

Alex Bagnolini
  • 21,990
  • 3
  • 41
  • 41
  • 1
    Somehow only this snippet worked for me. This had no effect `$( "#datepicker" ).datepicker({ dateFormat: "dd-mm-yy"});` – gkiko Dec 02 '15 at 12:45
8

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.

Matt Ball
  • 354,903
  • 100
  • 647
  • 710
  • Thanks man, But 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. – Mithun Sreedharan Mar 11 '10 at 18:36
  • Well, you can read that out of the input yourself: `defaultDate: $('#datePicker').val()`. – Matt Ball Mar 11 '10 at 19:05
0

mm equals minutes not months.
Use dd/MM/yyyy instead.

Yeldar Kurmangaliyev
  • 33,467
  • 12
  • 59
  • 101
0

Also remember that type="date" will make issue, so set type as text.

Bishal Paudel
  • 1,896
  • 2
  • 21
  • 28
-5

Not accurate!!!

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

You must to put the right format in dateFormat : for your example there is : dd/mm/yy two mistake:

  • day/month inversion
  • year is only two 'y' not four
Shawn Chin
  • 84,080
  • 19
  • 162
  • 191
nabla
  • 11