在 jquery datepicker 中设置最小日期

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10139378/
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 08:52:24  来源:igfitidea点击:

setting min date in jquery datepicker

jqueryjquery-uijquery-ui-datepicker

提问by user1184777

Hi i want to set min date in my jquery datepicker to (1999-10-25) so i tried the below code its not working.

嗨,我想将 jquery datepicker 中的最小日期设置为 (1999-10-25),所以我尝试了以下代码,但它不起作用。

$(function () {
    $('#datepicker').datepicker({
        dateFormat: 'yy-mm-dd',
        showButtonPanel: true,
        changeMonth: true,
        changeYear: true,
        showOn: "button",
        buttonImage: "images/calendar.gif",
        buttonImageOnly: true,
        minDate: new Date(1999, 10 - 1, 25),
        maxDate: '+30Y',
        inline: true
    });
});

** if i change the min year to above 2002 than it will work fine but if i specify min year less than 2002{like above eexample 1999} it will show only up to 2002.can someone help me. i am using jquery-1.7.1.min.js and jquery-ui-1.8.18.custom.min.js.

** 如果我将最小年份更改为 2002 年以上,它会正常工作,但如果我指定最小年份小于 2002 {like above eexample 1999} 它只会显示到 2002。有人可以帮助我。我正在使用 jquery-1.7.1.min.js 和 jquery-ui-1.8.18.custom.min.js。

回答by Gaurav

$(function () {
    $('#datepicker').datepicker({
        dateFormat: 'yy-mm-dd',
        showButtonPanel: true,
        changeMonth: true,
        changeYear: true,
yearRange: '1999:2012',
        showOn: "button",
        buttonImage: "images/calendar.gif",
        buttonImageOnly: true,
        minDate: new Date(1999, 10 - 1, 25),
        maxDate: '+30Y',
        inline: true
    });
});

Just added year range option. It should solve the problem

刚刚添加了年份范围选项。应该可以解决问题

回答by Marco Johannesen

The problem is that the default option of "yearRange" is 10 years.

问题是“yearRange”的默认选项是 10 年。

So 2012 - 10 = 2002.

所以2012 - 10 = 2002

So change the yearRange to c-20:cor just 1999 (yearRange: '1999:c'), and use that in combination with restrict dates (mindate, maxdate).

因此,将 yearRange 更改为c-20:c或仅更改为1999 ( yearRange: '1999:c'),并将其与限制日期(mindate、maxdate)结合使用。

For more info: http://jqueryui.com/demos/datepicker/#option-yearRange

更多信息:http: //jqueryui.com/demos/datepicker/#option-yearRange



See example: http://jsfiddle.net/kGjdL/

参见示例:http: //jsfiddle.net/kGjdL/

And your code with the addition:

还有你的代码:

$(function () {
    $('#datepicker').datepicker({
        dateFormat: 'yy-mm-dd',
        showButtonPanel: true,
        changeMonth: true,
        changeYear: true,
        showOn: "button",
        buttonImage: "images/calendar.gif",
        buttonImageOnly: true,
        minDate: new Date(1999, 10 - 1, 25),
        maxDate: '+30Y',
        yearRange: '1999:c',
        inline: true
    });
});

回答by Tats_innit

Hiya working demo: http://jsfiddle.net/femy8/

Hiya工作演示http: //jsfiddle.net/femy8/

Now the calendar will only go to minimum of 1999-10-25.

现在日历将只到 1999-10-25 的最小值。

Click on the image i.e. small icon next to text box for calendar to appear. You can try selecting up until 1999 but the minimum date for selection is 25th of oct 1999. which is what you want.

单击图像,即文本框旁边的小图标以显示日历。您可以尝试选择直到 1999 年,但选择的最短日期是 1999 年 10 月 25 日。这正是您想要的。

This will help, have a nice one! :) cheers!

这会有所帮助,祝你好运!:) 干杯!

Jquery Code

查询代码

$(".mypicker").datepicker({
    changeYear: true,
    dateFormat: 'yy-mm-dd',
    showButtonPanel: true,
    changeMonth: true,
    changeYear: true,
    showOn: "button",
        buttonImage: "images/calendar.gif",
        buttonImageOnly: true,
        minDate: new Date('1999/10/25'),
        maxDate: '+30Y',
        inline: true
});


?

回答by rthonz

Just want to add this for the future programmer.

只是想为未来的程序员添加这个。

This code limits the date min and max. The year is fully controlled by getting the current year as max year.

此代码限制日期最小值和最大值。通过将当前年份作为最大年份来完全控制年份。

Hope this could help to anyone.

希望这可以帮助任何人。

Here's the code.

这是代码。

var dateToday = new Date();
var yrRange = '2014' + ":" + (dateToday.getFullYear());

$(function () {
    $("[id$=txtDate]").datepicker({
        showOn: 'button',
        changeMonth: true,
        changeYear: true,
        showButtonPanel: true,
        buttonImageOnly: true,
        yearRange: yrRange,
        buttonImage: 'calendar3.png',
        buttonImageOnly: true,
        minDate: new Date(2014,1-1,1),
        maxDate: '+50Y',
        inline:true
    });
});

回答by Ezequiel García

Just in case if for example you need to put a min date, the last 3 months and max date next 3 months

以防万一,例如,您需要输入最小日期、过去 3 个月和接下来 3 个月的最大日期

$('#id_your_date').datepicker({ 
   maxDate: '+3m',
   minDate: '-3m'
 });

回答by kanhaiya kumar

Try like this

像这样尝试

<script>

  $(document).ready(function(){
          $("#order_ship_date").datepicker({
           changeMonth:true,
           changeYear:true,           
            dateFormat:"yy-mm-dd",
            minDate: +2,
        });
  }); 


</script>

html code is given below

html代码如下

<input id="order_ship_date"  type="text" class="input" style="width:80px;"  />

回答by Roshan

basically if you already specify the year range there is no need to use mindateand maxdateif only year is required

基本上,如果您已经指定了年份范围,则无需使用mindatemaxdate如果只需要年份