jQueryUI DatePicker YearRange and ChangeYear problems

CoreyT picture CoreyT · Dec 22, 2010 · Viewed 35.9k times · Source

I have a datepicker on an application form. We only allow applications where the date of birth is within a certain age range. I enabled ChangeYear and set the YearRange to "-65:-16". The problems I am having are as follows:

1 - When I select a date without first selecting something in the year dropdown, I get the correct month and day, but I get 2016 as a year.

2 - Trying to fix this I set the YearRange to "n-65:n-16". That causes the year dropdown to only display the current year (2010). Even weirder is that if you select a date, you still get the correct month and day, and the year 2016.

Here is the code I use to setup the datepicker:

    <script type="text/javascript">
    $(document).ready(function (e) {
        Sys.WebForms.PageRequestManager.getInstance().add_endRequest(EndRequestHandler);

        function EndRequestHandler(sender, args) {
            $(function () {
                $("#DateOfBirth").datepicker({ yearRange: '-65:-13', changeMonth: true, changeYear: true, defaultDate: '1-1-1994', dateFormat: 'mm-dd-yy' })
            });
        }

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

I am hoping this is something I have done wrong, and someone can tell me what that is. Thanks for any help.

Answer

Karsten picture Karsten · Oct 24, 2012

Try using 'c' for the current year like:

$('.datepicker').datepicker({changeYear: true, yearRange : 'c-65:c+10'});

for more information view http://api.jqueryui.com/datepicker/#option-yearRange