开发者

How do I set start year and end year of jquery date picker?

开发者 https://www.devze.com 2023-02-25 05:48 出处:网络
When I initialize a jquery date picker like $( \"#datepicker\" ).datepicker({ changeMonth: true, changeYear: true

When I initialize a jquery date picker like

$( "#datepicker" ).datepicker({
    changeMonth: true,
    changeYear: true
});

The showing the date picker from year 2000 to year 2020.

EDIT:

When i 开发者_运维百科go to year 2000, the next time it shows from 1990, but any way I want to customize it.

How do I set the from year and to year that will fit for my requirements.

Thanks in advance.


$(function() {
    $( "#datepicker" ).datepicker({
      changeMonth: true,
      changeYear: true,
      yearRange: '1945:'+(new Date).getFullYear()         
    });
});

this example for year start from 1945 to current year


$( "#datepicker" ).datepicker({
        changeMonth: true,
        changeYear: true,
        yearRange: '1972:2011'
    });

Should be work well.


See the documentation: The option is called yearRange.

Control the range of years displayed in the year drop-down: either relative to today's year (-nn:+nn), relative to the currently selected year (c-nn:c+nn), absolute (nnnn:nnnn), or combinations of these formats (nnnn:-nn). Note that this option only affects what appears in the drop-down, to restrict which dates may be selected use the minDate and/or maxDate options.


Try with this:

$('#license_expiry_date').datepicker({changeMonth: true, 
                                      changeYear: true, 
                                      yearRange: '-100:+100',
                                      maxDate: new Date(2100, 1,18) });


You could also do something like following:

'yearRange': '1945:' - End year is the current one.

'yearRange': '1945:+10 - Add +10 years to the current one.

'yearRange': '1945:-10 - Subtract -10 years from the current one.


start date must be limit at most 2 year selection

 $(document).on('change', '#startdate', function () {
        var current=$(this).val();
        var currentYear = current.split("-");
        var compareYear=(new Date().getFullYear()-1);
            if(compareYear > currentYear[0]){
                $('#limitMsg').text('this year must at most one year from current date');
                $('#submit').prop('disabled', true);
            }else{
                $('#limitMsg').text('');
                $('#submit').prop('disabled', false);
            }
  });
0

精彩评论

暂无评论...
验证码 换一张
取 消