Search code examples
javascriptdaterangepicker

daterangepicker set no dates


I have a daterangepicker (http://www.daterangepicker.com/) setup on my site:

enter image description here

With this code:

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

When I select "None" I'd like it to set the field to blank, but instead it sets the value to "Invalid Date - Invalid Date":

enter image description here

Is there a way to blank out the field based on the selection?


Solution

  • Using 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" />