Range picker with presets

Build complex pickers to show within popups to make things easy for your users. A great example is date-range selection with presets. If you've ever used Google Analytics, you have probably seen the date range selector for filtering the data, which is very similar to what you can see in this example. Select custom start and end dates along with presets is helpful for easy access to predefined ranges that people are usually interested in.

Would like to explore more date & time picker functionality?  Check out the range examples we have for you →
Get started with this example

Demo

Loading the live demo

Preview in full screen

Code

import { CommonModule } from '@angular/common';
import { AfterViewInit, Component, OnInit, ViewChild } from '@angular/core';
import { FormsModule } from '@angular/forms';
import {
  formatDate,
  MbscDatepickerOptions,
  MbscDateType,
  MbscInput,
  MbscModule,
  MbscPopup,
  MbscPopupOptions,
  MbscSelectData,
  MbscSelectOptions,
  options,
  setOptions ,
} from '@mobiscroll/angular';

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

const now = new Date();
const day = now.getDay();
const monday = now.getDate() - day + (day === 0 ? -6 : 1);
const startDate = '2026-10-27T00:00';
const endDate = '2026-11-02T00:00';

@Component({
  selector: 'app-root', 
  templateUrl: './app.component.html',
  standalone: true,
  imports: [CommonModule, FormsModule, MbscModule],
})
export class AppComponent implements OnInit, AfterViewInit {
  @ViewChild('popup', { static: false })
  popup!: MbscPopup;

  @ViewChild('input', { static: false })
  input!: MbscInput;

  inputElm!: HTMLElement;
  dateInput = '';
  selected = 'custom';
  selectedDate: MbscDateType[] = [startDate, endDate];
  disableInput = false;

  respPopup: Record<string, MbscPopupOptions & { breakpoint?: number }> = {
    xsmall: {
      display: 'bottom',
      touchUi: true,
      buttons: [
        {
          text: 'Apply',
          handler: () => {
            this.changeInputValue(this.selectedDate[0] as string, (this.selectedDate[1] || this.selectedDate[0]) as string);
            this.popup.close();
          },
        },
        'cancel',
      ],
    },
    custom: {
      breakpoint: 559,
      buttons: [],
      display: 'anchored',
      anchorAlign: 'start',
      touchUi: false,
      scrollLock: false,
      showArrow: false,
      maxWidth: 920,
    },
  };

  respSelect = {
    xsmall: {
      touchUi: true,
    },
    small: {
      touchUi: false,
    },
  };

  myData: MbscSelectData[] = [
    {
      value: 'custom',
      text: 'Custom',
    },
    {
      value: 'today',
      text: 'Today',
    },
    {
      value: 'yesterday',
      text: 'Yesterday',
    },
    {
      value: 'last-week',
      text: 'Last week',
    },
    {
      value: 'last-month',
      text: 'Last month',
    },
    {
      value: 'last-7-days',
      text: 'Last 7 days',
    },
    {
      value: 'last-30-days',
      text: 'Last 30 days',
    },
  ];

  selectOptions: MbscSelectOptions = {
    responsive: {
      xsmall: {
        touchUi: true,
      },
      small: {
        touchUi: false,
      },
    },
    onChange: (args) => {
      const s = args.value;

      if (s === 'custom') {
        this.disableInput = false;
      } else {
        this.disableInput = true;

        switch (s) {
          case 'today':
            this.selectedDate = ['2021-09-15T00:00', '2021-09-15T00:00'];
            break;
          case 'yesterday':
            this.selectedDate = ['2021-09-14T00:00', '2021-09-14T00:00'];
            break;
          case 'last-week':
            this.selectedDate = [
              new Date(now.getFullYear(), now.getMonth(), monday - 7),
              new Date(now.getFullYear(), now.getMonth(), monday - 1),
            ];
            break;
          case 'last-month':
            this.selectedDate = ['2021-08-01T00:00', '2021-08-31T00:00'];
            break;
          case 'last-7-days':
            this.selectedDate = ['2021-09-09T00:00', '2021-09-15T00:00'];
            break;
          case 'last-30-days':
            this.selectedDate = ['2021-08-17T00:00', '2021-09-15T00:00'];
            break;
          default:
            break;
        }
      }
      this.selected = s;
    },
  };

  calendarOptions: MbscDatepickerOptions = {
    controls: ['calendar'],
    select: 'range',
    display: 'inline',
    showRangeLabels: false,
    pages: 'auto',
    returnFormat: 'iso8601',
    showOnClick: false,
    showOnFocus: false,
    onChange: () => {
      this.disableInput = false;
    },
  };

  inputClick(): void {
    this.popup.open();
  }

  changeInputValue(start: string, end: string): void {
    const locale = options.locale || {};
    const dateFormat = locale.dateFormat || 'DD/MM/YYYY';

    this.dateInput = formatDate(dateFormat, new Date(start)) + ' - ' + formatDate(dateFormat, new Date(end));
  }

  applyClick(): void {
    const date = this.selectedDate;

    this.changeInputValue(date[0] as string, (date[1] || date[0]) as string);
    this.popup.close();
  }

  cancelClick(): void {
    this.popup.close();
  }

  ngOnInit(): void {
    this.changeInputValue(startDate, endDate);
  }

  ngAfterViewInit(): void {
    setTimeout(() => {
      this.inputElm = this.input.nativeElement;
    });
  }
}

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.