Javascript 使用 jQuery datepicker 创建特定的日期范围

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

Create a specific date range with jQuery datepicker

javascriptjqueryjquery-uicalendardatepicker

提问by bigmike7801

I'm trying to use the jQuery date picker to create a start date calendar and an end date calender. I'm using the "date range" example seen here: http://jqueryui.com/demos/datepicker/#date-range

我正在尝试使用 jQuery 日期选择器来创建开始日期日历和结束日期日历。我正在使用此处看到的“日期范围”示例:http: //jqueryui.com/demos/datepicker/#date-range

The start date can not be before today's date and the end date can be 30 days past the selected start date.

开始日期不能早于今天的日期,结束日期可以是所选开始日期之后的 30 天。

For example if I chose a start date of May 17th in the first datepicker, then the end date in the second datepicker can only be selectable for May 18th through June 18th.

例如,如果我在第一个日期选择器中选择了 5 月 17 日的开始日期,那么第二个日期选择器中的结束日期只能选择 5 月 18 日至 6 月 18 日。

Here's my code:

这是我的代码:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="utf-8" />    
    <title>Untitled Document</title>
    <link href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/themes/base/jquery-ui.css" rel="stylesheet" type="text/css"/>
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.5/jquery.min.js"></script>
    <script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/jquery-ui.min.js"></script>

<script type="text/javascript">
    $(function() {
        var dates = $( "#from, #to" ).datepicker({
            defaultDate: "+1w",
            changeMonth: true,
            numberOfMonths: 2,
            onSelect: function( selectedDate ) {
                var option = this.id == "from" ? "minDate" : "maxDate",
                    instance = $( this ).data( "datepicker" ),
                    date = $.datepicker.parseDate(
                        instance.settings.dateFormat ||
                        $.datepicker._defaults.dateFormat,
                        selectedDate, instance.settings );
                dates.not( this ).datepicker( "option", option, date );
            }
        });
    });
    </script>
  </head>

<body>
<div class="date">

<label for="from">From</label>
<input type="text" id="from" name="from"/>
<label for="to">to</label>
<input type="text" id="to" name="to"/>

</div><!-- End demo -->

</html>

Any help would be appreciated. Thanks!

任何帮助,将不胜感激。谢谢!

回答by c0mm0n

There you go : http://jsfiddle.net/c0mm0n/SJhmF/3/

你去吧:http: //jsfiddle.net/c0mm0n/SJhmF/3/

$(function() {
    $( "#from, #to" ).datepicker({
        defaultDate: "+1w",
        changeMonth: true,
        numberOfMonths: 3,
        onSelect: function( selectedDate ) {
            if(this.id == 'from'){
              var dateMin = $('#from').datepicker("getDate");
              var rMin = new Date(dateMin.getFullYear(), dateMin.getMonth(),dateMin.getDate() + 1); // Min Date = Selected + 1d
              var rMax = new Date(dateMin.getFullYear(), dateMin.getMonth(),dateMin.getDate() + 31); // Max Date = Selected + 31d
              $('#to').datepicker("option","minDate",rMin);
              $('#to').datepicker("option","maxDate",rMax);                    
            }

        }
    });
});

回答by James Montagne

This should do it:

这应该这样做:

http://jsfiddle.net/abze4/

http://jsfiddle.net/abze4/

$(function() {
    var fromDate = $("#from").datepicker({
        defaultDate: "+1w",
        changeMonth: true,
        numberOfMonths: 2,
        minDate: new Date(),
        onSelect: function(selectedDate) {
            var instance = $(this).data("datepicker");
            var date = $.datepicker.parseDate(instance.settings.dateFormat || $.datepicker._defaults.dateFormat, selectedDate, instance.settings);
            date.setDate(date.getDate()+30);
            toDate.datepicker("option", "minDate", date);
        }
    });

    var toDate = $("#to").datepicker({
        defaultDate: "+1w",
        changeMonth: true,
        numberOfMonths: 2
    });
});

Basically this sets the from date's minDate to today. Then when you select a fromDate, it sets the minDate of the toDate to the selected date +30.

基本上这将起始日期的 minDate 设置为今天。然后,当您选择 fromDate 时,它​​将 toDate 的 minDate 设置为所选日期 +30。