将日期范围日历限制为提前一年?

Limit daterange calender to one year ahead?

我继承了一个网站,尽管该网站上的日期范围选择器运行良好 - 客户现在需要将日期范围限制为从今天开始提前一年。

如何编辑现有代码以包含此功能?

      $(function () {
    var today = new Date();
    var tomorrow = new Date();
    tomorrow.setDate(tomorrow.getDate() + 1);

    arriving = today.getFullYear() + '-' + (today.getMonth() + 1) + '-' + today.getDate();
    departing = tomorrow.getFullYear() + '-' + (tomorrow.getMonth() + 1) + '-' + tomorrow.getDate();

    $('input#datepicker').daterangepicker({
        autoApply: true,
        startDate: today, // preselect dates
        endDate: tomorrow, locale: {
            format: "DD MMM YYYY"
        },
        minDate: today
    }, function (start, end, label) {
        if (start.isSame(end, 'day')) {
            end.add(1, 'days');
            $('#datepicker').data('daterangepicker').setEndDate(end);
        }
        arriving = start.format('YYYY-MM-DD');
        departing = end.format('YYYY-MM-DD');

    });

});

欢迎使用 Whosebug。您可以将 maxDate 设置为从今天起一年。您可以尝试以下

$(function () {
        var today = new Date();
        var tomorrow = new Date();
        tomorrow.setDate(tomorrow.getDate() + 1);

        arriving = today.getFullYear() + '-' + (today.getMonth() + 1) + '-' + today.getDate();
        departing = tomorrow.getFullYear() + '-' + (tomorrow.getMonth() + 1) + '-' + tomorrow.getDate();

        $('input#datepicker').daterangepicker({
            autoApply: true,
            startDate: today, // preselect dates
            endDate: tomorrow, locale: {
                format: "DD MMM YYYY"
            },
            minDate: today,
            maxDate: new Date(new Date().setFullYear(new Date().getFullYear() + 1))
        }, function (start, end, label) {
            if (start.isSame(end, 'day')) {
                end.add(1, 'days');
                $('#datepicker').data('daterangepicker').setEndDate(end);
            }
            arriving = start.format('YYYY-MM-DD');
            departing = end.format('YYYY-MM-DD');

        });