Date Filter with Preset Ranges

When building a date filtering UI a date range picker is essential. On top of being able to select custom start and end dates presets can be very helpful and provide a nice productivity boost. A more advanced filtering experience can be created in a couple of steps:

  • Custom popup with an inline calendar - Create a custom popup and place an inline date picker with start/end selection
  • Select with common presets - Populate a dropdown with values like Today, Yesterday, Last week, Last month, Last 7 days, Last 30 days
  • Inputs for typing dates - Adding two inputs next to the range picker enables users to type in a date rather than solely relying on manual selection
  • Make it fully responsive - Use the responsive api to customize the desktop experience - dropdown with inputs for typing - and the mobile experience - touch optimized interface
Download and try example

Demo

Loading the live demo

Preview in full screen

Code

mobiscroll.setOptions({
  locale:  ,
  theme: ' ',
  themeVariant: ' '
});

var formatDate = mobiscroll.formatDate;
var startDate = '2026-10-23T00:00';
var endDate = '2026-10-29T00:00';
var dateInput = document.getElementById('date-filtering');
var startInput = document.getElementById('date-filtering-start');
var endInput = document.getElementById('date-filtering-end');

var now = new Date();
var day = now.getDay();
var monday = now.getDate() - day + (day === 0 ? -6 : 1);

var select = mobiscroll.select('#date-filtering-select', {
  inputElement: document.getElementById('date-select-input'),
  responsive: {
    xsmall: {
      touchUi: true,
    },
    small: {
      touchUi: false,
    },
  },
  onChange: function (args) {
    var selected = args.value;

    if (selected === 'custom') {
      disableInputs(false);
    } else {
      disableInputs(true);

      switch (selected) {
        case 'today':
          calendar.setVal(['2026-10-23T00:00', '2026-10-23T00:00']);
          break;
        case 'yesterday':
          calendar.setVal(['2026-10-22T00:00', '2026-10-22T00:00']);
          break;
        case 'last-week':
          calendar.setVal([
            new Date(now.getFullYear(), now.getMonth(), monday - 7),
            new Date(now.getFullYear(), now.getMonth(), monday - 1),
          ]);
          break;
        case 'last-month':
          calendar.setVal(['2026-09-01T00:00', '2026-09-30T00:00']);
          break;
        case 'last-7-days':
          calendar.setVal(['2026-10-17T00:00', '2026-10-23T00:00']);
          break;
        case 'last-30-days':
          calendar.setVal(['2026-09-24T00:00', '2026-10-23T00:00']);
          break;
      }
    }
  },
});

function disableInputs(disable) {
  var startInst = mobiscroll.getInst(startInput);
  var endInst = mobiscroll.getInst(endInput);

  startInst.setOptions({ disabled: disable });
  endInst.setOptions({ disabled: disable });
}

var calendar = mobiscroll.datepicker('#date-filtering-calendar', {
  controls: ['calendar'],
  select: 'range',
  display: 'inline',
  showRangeLabels: false,
  pages: 'auto',
  startInput: '#date-filtering-start',
  endInput: '#date-filtering-end',
  returnFormat: 'iso8601',
  showOnClick: false,
  showOnFocus: false,
  onInit: function (event, inst) {
    inst.setVal([startDate, endDate]);
    setInputValue(startDate, endDate, inst.s.dateFormat);
  },
  onChange: function () {
    disableInputs(false);
    select.setVal('custom');
  },
});

var popup = mobiscroll.popup('#date-filtering-popup', {
  responsive: {
    xsmall: {
      display: 'bottom',
      touchUi: true,
      buttons: [
        {
          text: 'Apply',
          handler: function () {
            var date = calendar.getVal();

            setInputValue(date[0], date[1] || date[0], calendar.s.dateFormat);
            popup.close();
          },
        },
        'cancel',
      ],
    },
    custom: {
      breakpoint: 559,
      buttons: [],
      display: 'anchored',
      anchor: dateInput,
      anchorAlign: 'start',
      touchUi: false,
      scrollLock: false,
      showArrow: false,
      maxWidth: 920,
    },
  },
});

function setInputValue(start, end, dateFormat) {
  dateInput.value = formatDate(dateFormat, new Date(start)) + ' - ' + formatDate(dateFormat, new Date(end));
}

dateInput.addEventListener('click', function () {
  popup.open();
});

document.addEventListener('click', function (e) {
  if (e.target && e.target.classList.contains('apply-button')) {
    var date = calendar.getVal();

    setInputValue(date[0], date[1] || date[0], calendar.s.dateFormat);
    popup.close();
  } else if (e.target && e.target.classList.contains('cancel-button')) {
    popup.close();
  }
});
  

Duplicate Session

You have signed in on a different machine. To keep everything in sync, you have been signed out of this session.

If you need to invite team members, manage seats and accounts you can do it from the licenses page.

Duplicate Session

We noticed you are logged in on a different machine and have closed your old session.

If you need to invite team members, manage seats and accounts you can do it from the licenses page.