logo dile-components dile-components

dile-date-range-picker

Web Component to select a range of dates (a start and an end) in a popup dile-calendar in range mode. It can offer quick ranges like "Today", "Last 7 days" or "This month", and it works as a form field: inside a DileForm, in native forms and with the required check. To select a single date, use dile-datepicker.

Installation

npm i @dile/utils

Usage

Import the component.

import '@dile/utils/components/datepicker/date-range-picker.js';

Use the component.

<dile-date-range-picker name="period" label="Period"></dile-date-range-picker>

Select the field (or press Arrow Down on it) to open the calendar. The first day selected is the start of the range and the second one the end; if the second day is before the first one, the dates are swapped. When the end is selected the calendar closes and the field shows the range. Closing the calendar after choosing only the start keeps the previous value.

Value

The value is a string with the start and the end in YYYY-MM-DD format separated by a slash (the ISO 8601 notation for intervals), or an empty string when there is no range:

picker.value; // "2026-03-02/2026-03-15"
picker.start; // "2026-03-02"
picker.end;   // "2026-03-15"

Set it from JavaScript with value or with setRange(start, end), which accepts Date objects or YYYY-MM-DD strings in any order:

picker.setRange(new Date(2026, 2, 2), '2026-03-15');

Two months

Set visibleMonths="2" to show two months side by side, handy for ranges that cross the end of a month. The calendar opens with the month of the start of the range first, and the selection, the preview and the keyboard work across both months (see dile-calendar).

<dile-date-range-picker label="Stay" visibleMonths="2"></dile-date-range-picker>

Two months don't fit on narrow screens, so the picker shows one month while the viewport is narrower than multipleMonthsMinWidth (640 pixels by default). It follows the viewport with a media query, so it changes when the window is resized or the device is rotated, even with the calendar open. effectiveVisibleMonths says how many months are being shown. With two months the popup is wider: --dile-date-range-picker-multiple-months-width, min(680px, 100vw - 25px) by default.

<dile-date-range-picker label="Stay" visibleMonths="2" multipleMonthsMinWidth="800"></dile-date-range-picker>

Quick ranges

Add quick ranges with the quick-ranges attribute, a comma-separated list of built-in names. They are shown as buttons above the calendar:

<dile-date-range-picker label="Report period" quick-ranges="today,last7Days,thisMonth,lastMonth"></dile-date-range-picker>

Built-in quick ranges: today, yesterday, last7Days, last30Days (both ending today), thisWeek, lastWeek (they use firstDayOfWeek), thisMonth, lastMonth, thisYear and lastYear. Their labels are localized (English, Spanish, Catalan, French and German).

From JavaScript, the quickRanges property also accepts custom ranges, mixed with the built-in names: an object with a label and fixed start / end dates (Date or YYYY-MM-DD), or with a range(today) function that returns { start, end }:

picker.quickRanges = [
  'thisMonth',
  { label: 'Q1 2026', start: '2026-01-01', end: '2026-03-31' },
  { label: 'Next 14 days', range: (today) => ({ start: today, end: new Date(today.getFullYear(), today.getMonth(), today.getDate() + 13) }) },
];

Limits and disabled dates

minDate, maxDate and disableDates work as in dile-datepicker:

<dile-date-range-picker label="Stay" minDate="2026-01-01" maxDate="2026-12-31"></dile-date-range-picker>
// No range can start or end on a Tuesday
picker.disableDates = (date) => date.getDay() === 2;

The days disabled by disableDates can be inside a range, but they can't be its start or its end (a stay can include a day when check-ins are closed). If a range must not contain disabled days, check it with a validation rule of the form.

Localization

The calendar, the format of the dates shown and the texts of the picker follow the locale property or, without it, the lang of the page (<html lang>), or en-GB when the page has none.

<dile-date-range-picker label="Periodo" locale="es-ES" firstDayOfWeek="1"></dile-date-range-picker>

Override the texts of the picker with messages, the texts of the calendar with calendarMessages (see dile-calendar) and the validation texts with validationMessages:

picker.messages = {
  placeholder: 'Choose your dates',
  dialogLabel: 'Choose your dates',
  quickRangesLabel: 'Suggestions',
  clear: 'Remove',
  last7Days: 'Past week',
};

The keys of messages are placeholder (text of the empty field), dialogLabel (accessible name of the calendar popup when the field has no label), quickRangesLabel (accessible name of the quick ranges group), clear (button that removes the range) and the names of the built-in quick ranges.

Validation

An empty value is valid unless the field is required. Inside a DileForm, validate() checks, in this order, the required attribute, the field's own getValidationError() and the rules of the form. The field reports the error and the form shows it:

The messages are in the language of the field and can be overridden with validationMessages ({date} is replaced by the limit date), or with the messages of the form by field and reason. The property of the field wins:

picker.validationMessages = { beforeMin: 'Choose a range from {date}' };
form.validationMessages = { 'period.afterMax': 'The period must end this year.' };

The field is form-associated and keeps its native validity up to date (checkValidity(), reportValidity(), validity), so it also works in a plain <form>. See DileNativeValidity.

Properties

Property Type Default Description
value String "" The range, "YYYY-MM-DD/YYYY-MM-DD", or "".
name String "" Name of the field in the form.
label String "" Visible label.
accessibleLabel String "" Accessible name of the field, used only when there is no visible label.
placeholder String "" Text of the empty field. Without it, the localized "Select a date range".
disabled Boolean false Disables the field. A disabled ancestor <fieldset> or <form> also disables it.
required Boolean false The field needs a range. Shows the required indicator in the label.
message String "" Help or error message shown below the field.
errored Boolean false Shows the field and its message as errored.
hideErrorOnInput Boolean false Removes the error when the user selects a range.
visibleMonths Number 1 Months shown side by side in the calendar. On narrow screens the picker shows one.
multipleMonthsMinWidth Number 640 Minimum width of the viewport, in pixels, to show more than one month.
quickRanges Array [] Quick ranges: built-in names and / or custom objects. Exposed via quick-ranges (comma-separated names).
locale String "" Language of the calendar, the dates and the texts. Default: the lang of the page or en-GB.
firstDayOfWeek Number 0 First day of the week, 0 (Sunday) to 6 (Saturday). Also used by thisWeek / lastWeek.
weekdayHeaderNotation String "short" Notation of the weekday names: "long", "short" or "narrow".
minDate Date | String — First selectable date (Date or "YYYY-MM-DD").
maxDate Date | String — Last selectable date.
disableDates Function — (date) => boolean, true for the days that can't be the start or the end. Only from JavaScript.
minYear Number current year - 100 First year of the year column of the calendar.
maxYear Number current year + 100 Last year of the year column of the calendar.
messages Object {} Overrides the texts of the picker.
validationMessages Object {} Overrides the validation texts (invalid, beforeMin, afterMax, disabled).
calendarMessages Object {} Overrides the texts of the calendar.
horizontalAlign String "under_left" Horizontal position of the popup (see dile-menu-overlay).
verticalAlign String "bottom" Vertical position of the popup.
moveTop Number 0 Vertical offset of the popup.
moveLeft Number 0 Horizontal offset of the popup.
opened Boolean false true while the calendar is shown (read only, reflected as an attribute).

Read-only getters: effectiveVisibleMonths (months shown now: visibleMonths, or 1 on narrow screens), start and end ("YYYY-MM-DD" or ""), range ({ start, end } with Date objects, or null) and displayValue (the text shown in the field).

Methods

The module also exports the helpers parseDateRange(value) (returns { start, end } with Date objects, null for an empty value or undefined for an invalid one) and formatDateRange(start, end), plus QUICK_RANGES, DATE_RANGE_MESSAGES and DATE_RANGE_VALIDATION_MESSAGES, from @dile/utils/components/datepicker/index.js.

Events

picker.addEventListener('dile-date-range-changed', (e) => {
  const { start, end } = e.detail;
  loadReport(start, end);
});

CSS Custom Properties

The field uses the --dile-input-* custom properties of dile-input (border, background, color, padding, label, focus ring...), the popup the ones of dile-menu-overlay and the calendar the --dile-calendar-* ones of dile-calendar, including the range colors. In addition:

Custom property Description Default
--dile-date-range-picker-width Width of the popup min(350px, 100vw - 25px)
--dile-date-range-picker-multiple-months-width Width of the popup with several months min(680px, 100vw - 25px)
--dile-date-range-picker-font-size Font size of the calendar 0.9rem
--dile-date-range-picker-panel-padding Padding of the popup 0.5rem 0.25rem
--dile-date-range-picker-trigger-color Color of the calendar icon --dile-on-background-color or #303030
--dile-date-range-picker-trigger-size Size of the calendar icon 20px
--dile-date-range-picker-trigger-disabled-color Color of the icon when disabled #ccc
--dile-date-range-picker-quick-range-font-size Font size of the quick range buttons 0.8rem
--dile-date-range-picker-quick-range-border-radius Radius of the quick range buttons 1rem
--dile-date-range-picker-quick-range-border-color Border of the quick range buttons rgba(90, 90, 90, 0.35)
--dile-date-range-picker-quick-range-background-color Background of the quick range buttons transparent
--dile-date-range-picker-quick-range-text-color Text color of the quick range buttons inherit
--dile-date-range-picker-quick-range-hover-background-color Background of a hovered quick range rgba(90, 90, 90, 0.08)
--dile-date-range-picker-quick-range-selected-background-color Background of the quick range of the current value --dile-calendar-range-edge-background-color, --dile-primary-color or #2563eb
--dile-date-range-picker-quick-range-selected-text-color Text of the quick range of the current value --dile-calendar-range-edge-text-color, --dile-on-primary-color or #fff
--dile-date-range-picker-summary-font-size Font size of the range summary and the clear button 0.85rem
--dile-date-range-picker-clear-color Color of the clear button --dile-link-color or #2563eb

Accessibility

dile-date-range-picker demos

Basic usage

Detail of dile-date-range-changed:

<script type="module">
  import '@dile/utils/components/datepicker/date-range-picker.js';
  const picker = document.getElementById('basicRangePicker');
  picker.addEventListener('dile-date-range-changed', (e) => {
    document.getElementById('basicRangeOutput').textContent = JSON.stringify(e.detail);
  });
</script>
<dile-date-range-picker id="basicRangePicker" name="period" label="Period"></dile-date-range-picker>
<p>Detail of dile-date-range-changed: <code id="basicRangeOutput"></code></p>

Quick ranges

<dile-date-range-picker label="Report period" quick-ranges="today,yesterday,last7Days,last30Days,thisWeek,thisMonth,lastMonth,thisYear"></dile-date-range-picker>

Two months

On screens narrower than 640 pixels it shows one month.

<dile-date-range-picker label="Stay" visibleMonths="2" firstDayOfWeek="1" quick-ranges="thisWeek,thisMonth,lastMonth"></dile-date-range-picker>

Bookings from today, Monday first

The range can't start before today, and Tuesdays can't be the start or the end of the stay.

<script type="module">
  const booking = document.getElementById('bookingRangePicker');
  booking.minDate = new Date();
  booking.disableDates = (date) => date.getDay() === 2;
</script>
<dile-date-range-picker id="bookingRangePicker" label="Stay" firstDayOfWeek="1"></dile-date-range-picker>

Another language

<dile-date-range-picker label="Periodo" locale="es-ES" firstDayOfWeek="1" quick-ranges="today,last7Days,thisMonth" value="2026-03-02/2026-03-15"></dile-date-range-picker>

Inside a form

<script type="module">
  import { LitElement, html } from 'lit';
  import { DileForm } from '@dile/ui/mixins/form/index.js';

  class RangeFormDemo extends DileForm(LitElement) {
    static properties = { result: { state: true } };
    render() {
      return html`
        <dile-date-range-picker name="period" label="Period of 2026" required
          minDate="2026-01-01" maxDate="2026-12-31" quick-ranges="thisMonth,lastMonth"></dile-date-range-picker>
        <dile-button @click=${this.send}>Send</dile-button>
        <pre>${this.result}</pre>
      `;
    }
    async send() {
      const { valid, errors } = await this.validate();
      this.result = JSON.stringify(valid ? this.getData() : errors);
    }
  }
  customElements.define('range-form-demo', RangeFormDemo);
</script>
<range-form-demo></range-form-demo>

Styled

<style>
  .styled-range {
    --dile-input-label-color: #6d3d6c;
    --dile-input-label-font-weight: bold;
    --dile-input-border-color: #6d3d6c;
    --dile-input-border-width: 2px;
    --dile-input-background-color: #ffc;
    --dile-date-range-picker-trigger-color: #6d3d6c;
    --dile-calendar-range-edge-background-color: #6d3d6c;
    --dile-calendar-range-background-color: #ecd9ec;
    --dile-date-range-picker-quick-range-border-color: #6d3d6c;
  }
</style>
<dile-date-range-picker class="styled-range" label="Styled range" quick-ranges="thisWeek,lastWeek"></dile-date-range-picker>

Disabled

<dile-date-range-picker label="Disabled" disabled value="2026-03-02/2026-03-15"></dile-date-range-picker>