将daterange日历限制为提前一年?

时间:2019-04-19 11:46:08

标签: bootstrap-daterangepicker

我继承了一个网站,尽管该网站上的daterangepicker正常运行-客户现在需要将日期范围限制为今天的日期之前的一年。

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

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

    });

});

1 个答案:

答案 0 :(得分:0)

欢迎来到stackoverflow。您可以将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');

        });