jQuery 如何在日期选择器中设置 3 个月的日期范围?

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

How to set 3 months date range in date picker?

jqueryvalidationdatedatepicker

提问by CJ Ramki

I have from date and to date here. I want to set my maxDate to 3 months depends on the from date selection. If the user select from date 01-01-2014, #to maxDate should be next 3 months based on the from date, like (01-04-2014).

我在这里有从日期和至今。我想将 maxDate 设置为 3 个月,这取决于起始日期选择。如果用户从日期 01-01-2014 中选择,#to maxDate 应该是基于起始日期的下 3 个月,例如 (01-04-2014)。

$(function () {
    $("#from").datepicker({
        minDate: "dateToday",
        changeMonth: true,
        dateFormat: 'dd-mm-yy',

        onClose: function (selectedDate) {
            $("#to").datepicker("option", "minDate", selectedDate);
        }
    });
    $("#to").datepicker({
        minDate: "dateToday",
        changeMonth: true,
        dateFormat: 'dd-mm-yy',
        maxDate: selectedDate + "+3M", //I want to set condition like this

        onClose: function (selectedDate) {
            $("#from").datepicker("option", "maxDate", selectedDate);
        }
    });
});

give me some suggestion to solve my problem

给我一些建议来解决我的问题

回答by Tushar Gupta - curioustushar

Fiddle DEMO

小提琴演示

$(function () {
    $("#from").datepicker({
        minDate: "dateToday",
        changeMonth: true,
        dateFormat: 'dd-mm-yy',
        onClose: function (selectedDate, instance) {
            if (selectedDate != '') { //added this to fix the issue
                $("#to").datepicker("option", "minDate", selectedDate);
                var date = $.datepicker.parseDate(instance.settings.dateFormat, selectedDate, instance.settings);
                date.setMonth(date.getMonth() + 3);
                console.log(selectedDate, date);
                $("#to").datepicker("option", "minDate", selectedDate);
                $("#to").datepicker("option", "maxDate", date);
            }
        }
    });
    $("#to").datepicker({
        minDate: "dateToday",
        changeMonth: true,
        dateFormat: 'dd-mm-yy',
        onClose: function (selectedDate) {
            $("#from").datepicker("option", "maxDate", selectedDate);
        }
    });
});

回答by Marikkani Chelladurai

$(function() {
    $( "#datepicker" ).datepicker({
        numberOfMonths: 3,
        showButtonPanel: true
    });
});

Here is the Documentation

这是文档

$("#startDate").datepicker({
    minDate: 0,
    onSelect: function(selected) {
        var date = new Date(selected);
    date.setMonth(date.getMonth()+3);
        $("#endDate").datepicker("option","maxDate", date);
    }
});

Here is your resut man.... Cheers....

这是你的结果……干杯……