views:

45

answers:

4

Hi, I have a problem with the Jquery UI datepicker, I have searched and searcher and i didn't find the answer.I have the following code:

<script type="text/javascript">
$(function() {               
    $("#birthdate" ).datepicker({
        changeMonth: true,
        changeYear: true,
        yearRange: '1920:2010',
        dateFormat : 'dd-mm-yy',
        defaultDate: '01-01-1985'
    });
});
</script>

I want that when the user click in the #birthdate input that the current date selected to be 01-01-1985, now is the current date.Sorry for my dumb question.

+4  A: 

Try passing in a Date object instead. I can't see why it doesn't work in the format you have entered:

<script type="text/javascript">
$(function() {               
    $("#birthdate" ).datepicker({
        changeMonth: true,
        changeYear: true,
        yearRange: '1920:2010',
        dateFormat : 'dd-mm-yy',
        defaultDate: new Date(1985, 00, 01)
    });
});
</script>

http://jqueryui.com/demos/datepicker/#option-defaultDate

Specify either an actual date via a Date object or as a string in the current dateFormat, or a number of days from today (e.g. +7) or a string of values and periods ('y' for years, 'm' for months, 'w' for weeks, 'd' for days, e.g. '+1m +7d'), or null for today.

Codesleuth
I was having a similar problem to the OP in jQuery UI 1.7.3 - although in 1.8.4 it was fine (had to run 1.7.3 due to legacy system running jQuery 1.3.2) - using the date object fixed it all.
HorusKol
+1  A: 

You can try with bellow code.

which ll make the default date as your date.

$('#birthdate').datepicker("setDate", new Date(1985,01,01) );
Umakanta.Swain
A: 

Seeing that

Set the date to highlight on first opening if the field is blank. Specify either an actual date via a Date object or as a string in the current dateFormat, or a number of days from today (e.g. +7) or a string of values and periods ('y' for years, 'm' for months, 'w' for weeks, 'd' for days, e.g. '+1m +7d'), or null for today.

If the current dateFormat isn't recognize, you can still use the Date object ( useing new Date(year, month, day)

In your example, this should work (I didn't tested it) :

<script type="text/javascript">
$(function() {               
    $("#birthdate" ).datepicker({
        changeMonth: true,
        changeYear: true,
        yearRange: '1920:2010',
        dateFormat : 'dd-mm-yy',
        defaultDate: new Date(1985,01,01)
    });
});
</script>
Chouchenos
A: 

Thx guys, sorry for my late answer(lunch break), the code is working, it was a cache problem.Sorry for taking you time.

Alesio