JQuery UI DatePicker displaying wrong date

I have a jQuery UI DatePicker with the following parameters:

changeMonth: true,
changeYear: true,
yearRange: "-16:-1",
dateFormat: 'dd-mm-yy'

      

It only correctly displays the years 1996 to 2011. However, when I select a date for the first time, it displays strangely as 08-03-2012. 2012 is not even a select option in the datepicker, but it is the date that is then generated in my textbox.

If I then select the date again, it displays correctly - this only happens the first time.

Any ideas?

+3


source to share


2 answers


You can set the default date in your range like this:



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

      

+5


source


Here's another way to set a default date with the first year of the range.



<script type="text/javascript">
var default_date = new Date(); //Create a new date object
default_date.setYear(date.getYear() - 16); //Substract the current year with the first year of the range

$(function() {               
    $("#birthdate" ).datepicker({
        changeMonth: true,
        changeYear: true,
        yearRange: "-16:-1",
        dateFormat: 'dd-mm-yy',
        defaultDate: default_date 
    });
});
</script>

      

0


source







All Articles