【问题标题】:Limit daterange calender to one year ahead?将日期范围日历限制为提前一年?
【发布时间】:2019-09-09 16:40:57
【问题描述】:

我继承了一个网站,尽管该网站上的 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');

    });

});

【问题讨论】:

    标签: bootstrap-daterangepicker


    【解决方案1】:

    欢迎来到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');
    
            });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-08-03
      • 2010-10-10
      • 1970-01-01
      • 2018-03-03
      • 1970-01-01
      • 2012-06-02
      • 2011-08-10
      • 2011-07-17
      相关资源
      最近更新 更多