jQuery 如何设置jquery日期选择器的开始年份和结束年份?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5675747/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 19:37:40  来源:igfitidea点击:

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

jqueryjquery-ui

提问by Pekka

When I initialize a jquery date picker like

当我初始化一个 jquery 日期选择器时

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

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

显示从 2000 年到 2020 年的日期选择器。

EDIT:

编辑:

When i go to year 2000, the next time it shows from 1990, but any way I want to customize it.

当我到 2000 年时,下一次它从 1990 年开始显示,但我想以任何方式对其进行自定义。

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

我如何设置适合我的要求的年份和年份。

Thanks in advance.

提前致谢。

回答by Ansyori

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

this example for year start from 1945 to current year

这个例子是从 1945 年开始到当年

回答by Ariful Islam

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

Should be work well.

应该工作得很好。

回答by Pekka

See the documentation: The option is called yearRange.

请参阅文档:该选项被调用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.

控制年份下拉列表中显示的年份范围:相对于今天的年份 (-nn:+nn)、相对于当前选定的年份 (c-nn:c+nn)、绝对 (nnnn:nnnn) 或这些格式的组合 (nnnn:-nn)。请注意,此选项仅影响下拉列表中显示的内容,使用 minDate 和/或 maxDate 选项限制可以选择的日期。

回答by Ramkumar P

Try with this:

试试这个:

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

回答by Fabian Marz

You could also do something like following:

您还可以执行以下操作:

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

'yearRange': '1945:'- 结束年份是当前年份。

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

'yearRange': '1945:+10- 在当前的基础上增加 +10 年。

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

'yearRange': '1945:-10- 从当前年份减去 -10 年。

回答by Jasim Juwel

start date must be limit at most 2 year selection

开始日期必须限制在最多 2 年的选择中

 $(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);
            }
  });