3

I've got a jquery UI DatePicker with the following parameters:

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

It correctly displays only years 1996 till 2011. However, when I select a date for the first time, it's strangely displayed as 08-03-2012. 2012 is not even an option for selection in the datepicker, but this is the date which is then produced in my text box.

If I then select a date once again, it's correctly displayed - this only occurs for the first time.

Any ideas?

2 Answers 2

5

You can set a 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>
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks - I knew I could do that, but I didn't think that it would act in this weird way unless I specified a date.
The default date unless you specified a date is the current date
0

Here is another way to set the 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>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.