daterangepicker 没有设置日期

daterangepicker set no dates

我的站点上有一个 daterangepicker (http://www.daterangepicker.com/) 设置:

使用此代码:

$(function() {
    $('input[id="searchDateRange"]').daterangepicker({
        timePickerSeconds: true,
        autoApply: true,
        startDate: "${defaultStart}",
        endDate: "${defaultEnd}",
        timePicker: true,
        locale: {
            format: 'MM/DD/YYYY HH:mm:ss:SSS'
        },
        ranges: {
           'Past 24 Hours': [moment().subtract(1, 'days'), moment()],
           'Today': [moment().startOf('day'), moment().endOf('day')],
           'Yesterday': [moment().subtract(1, 'days').startOf('day'), moment().subtract(1, 'days').endOf('day')],
           'Last 7 Days': [moment().subtract(6, 'days'), moment()],
           'Last 30 Days': [moment().subtract(29, 'days'), moment()],
           'This Month': [moment().startOf('month'), moment().endOf('month')],
           'Last Month': [moment().subtract(1, 'month').startOf('month'), moment().subtract(1, 'month').endOf('month')],
           'None': [null, null]
        }
    });


    var history = document.querySelector('.history');
    if (history) {
        new Tablesort(history);
    }
});

当我 select "None" 我希望将字段设置为空白,但它会将值设置为 "Invalid Date - Invalid Date":

有没有办法根据 selection 来清空字段?

使用apply.daterangepicker event and moment's isValid()

$('input[id="searchDateRange"]').daterangepicker({
  timePickerSeconds: true,
  autoApply: true,
  //startDate: "${defaultStart}",
  //endDate: "${defaultEnd}",
  timePicker: true,
  locale: {
    format: 'MM/DD/YYYY HH:mm:ss:SSS'
  },
  ranges: {
    'Past 24 Hours': [moment().subtract(1, 'days'), moment()],
    'Today': [moment().startOf('day'), moment().endOf('day')],
    'Yesterday': [moment().subtract(1, 'days').startOf('day'), moment().subtract(1, 'days').endOf('day')],
    'Last 7 Days': [moment().subtract(6, 'days'), moment()],
    'Last 30 Days': [moment().subtract(29, 'days'), moment()],
    'This Month': [moment().startOf('month'), moment().endOf('month')],
    'Last Month': [moment().subtract(1, 'month').startOf('month'), moment().subtract(1, 'month').endOf('month')],
    'None': [null, null]
  }
});

$('input[id="searchDateRange"]').on('apply.daterangepicker', function(ev, picker) {
  if (!picker.startDate.isValid() || !picker.endDate.isValid()) {
    $(this).val('')
  }
});
<script type="text/javascript" src="https://cdn.jsdelivr.net/jquery/latest/jquery.min.js"></script>
<script type="text/javascript" src="https://cdn.jsdelivr.net/momentjs/latest/moment.min.js"></script>
<script type="text/javascript" src="https://cdn.jsdelivr.net/npm/daterangepicker/daterangepicker.min.js"></script>
<link rel="stylesheet" type="text/css" href="https://cdn.jsdelivr.net/npm/daterangepicker/daterangepicker.css" />

<input type="text" id="searchDateRange" />

虽然上面的答案是一个很好的解决方案,而且比这个更好。您也可以尝试此代码:-

$('input[id="searchDateRange"]').on('change', function(){
    if($(this).val() == 'Invalid date - Invalid date')
    {
        $(this).val(" ");
    }
});