Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQueryUI DatePicker YearRange and ChangeYear problems

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.

like image 850
CoreyT Avatar asked Dec 22 '10 14:12

CoreyT


People also ask

How can I set start and end dates for a datepicker?

Setting a start and end date Then add both lines in the JavaScript Code box e.g. var start_date = new Date(). increment('week', 1); var end_date = new Date(). increment('week', 5);

How do I make my datepicker always visible?

Simply call . datepicker() on a div instead of an input. (To display the datepicker embedded in the page instead of in an overlay.) Save this answer.

How do I change my datepicker size?

By default DatePicker has standard height and width (height: '30px' and width: '143px'). You can change this height and width by using height and width property respectively.


2 Answers

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

like image 106
Karsten Avatar answered Oct 28 '22 09:10

Karsten


Check your defaultDate setting. I can only see what you're describing when I change the default date to something outside of the specified year range (i.e. '1-1-2016'.) Everything else in the code you posted worked for me.

like image 40
Ben Koehler Avatar answered Oct 28 '22 10:10

Ben Koehler