Skip to content

Date range picker ​

A button that opens a range calendar and shows the chosen start and end dates.

  • Version 1.0.0
  • Bootstrap 5.3+
  • Requires Bootstrap CSS + Bootstrap JS bundle
  • Source on GitHub

Installation ​

bash
npx bootcn add date-range-picker
bash
pnpm dlx bootcn add date-range-picker
bash
yarn dlx bootcn add date-range-picker
bash
bunx --bun bootcn add date-range-picker

The CLI also installs the design tokens and these components: calendar, button, label. It prints the <link> and <script> tags to add to your layout.

Usage ​

Load calendar.js before date-range-picker.js. The form submits the two hidden inputs (YYYY-MM-DD). Set data-start and data-end on the calendar to start with a range, and data-min / data-max to limit it.

JavaScript API ​

  • data-bootcn-date-range-picker on the wrapper; inside the popover is a Calendar in range mode (data-mode="range", hidden inputs marked data-bootcn-calendar-input="start" and "end").
  • The trigger text shows the range (Intl.DateTimeFormat.formatRange, in the page language) once a start is chosen; the popover closes when the end is chosen.
  • Bootcn.dateRangePicker.init(root) / destroy().

Accessibility ​

  • The trigger's name is its label plus the current range ("Stay, 20 – 27 Jan 2027").
  • Inside the calendar, days in the range have aria-selected="true"; keys are the same as the Calendar's. Choosing the end closes the popover and returns focus to the trigger; Escape closes it too.

Dependencies ​

  • Bootstrap 5.3+ CSS and the JS bundle.
  • themes/tokens/tokens.css.
  • The calendar component.
  • The button component.
  • The label component.

Source ​

The exact files the CLI installs (date-range-picker@1.0.0). View on GitHub · Report an issue

html
<div class="dropdown bootcn-date-picker bootcn-date-range-picker" data-bootcn-date-range-picker>
  <span class="bootcn-label" id="bootcn-date-range-picker-label">Stay</span>
  <button
    type="button"
    class="btn bootcn-button bootcn-button--outline bootcn-date-picker__trigger"
    data-bs-toggle="dropdown"
    data-bs-auto-close="outside"
    aria-expanded="false"
    aria-haspopup="dialog"
    aria-labelledby="bootcn-date-range-picker-label bootcn-date-range-picker-text"
  >
    <svg
      class="bootcn-icon"
      width="16"
      height="16"
      viewBox="0 0 24 24"
      fill="none"
      stroke="currentColor"
      stroke-width="2"
      stroke-linecap="round"
      stroke-linejoin="round"
      aria-hidden="true"
      focusable="false"
    >
      <rect x="3" y="5" width="18" height="16" rx="2" />
      <path d="M16 3v4M8 3v4M3 11h18" />
    </svg>
    <span id="bootcn-date-range-picker-text" data-bootcn-date-range-picker-text
      >Pick check-in and check-out</span
    >
  </button>
  <div
    class="dropdown-menu bootcn-date-picker__popover"
    role="dialog"
    aria-label="Choose check-in and check-out dates"
  >
    <div class="bootcn-calendar bootcn-calendar--range" data-bootcn-calendar data-mode="range">
      <div class="bootcn-calendar__header">
        <button
          type="button"
          class="bootcn-calendar__nav"
          data-bootcn-calendar-prev
          aria-label="Previous month"
        >
          <svg
            class="bootcn-icon"
            width="16"
            height="16"
            viewBox="0 0 24 24"
            fill="none"
            stroke="currentColor"
            stroke-width="2"
            stroke-linecap="round"
            stroke-linejoin="round"
            aria-hidden="true"
            focusable="false"
          >
            <path d="m15 18-6-6 6-6" />
          </svg>
        </button>
        <h2
          class="bootcn-calendar__title"
          id="bootcn-date-range-picker-calendar-title"
          aria-live="polite"
        >
          Calendar
        </h2>
        <button
          type="button"
          class="bootcn-calendar__nav"
          data-bootcn-calendar-next
          aria-label="Next month"
        >
          <svg
            class="bootcn-icon"
            width="16"
            height="16"
            viewBox="0 0 24 24"
            fill="none"
            stroke="currentColor"
            stroke-width="2"
            stroke-linecap="round"
            stroke-linejoin="round"
            aria-hidden="true"
            focusable="false"
          >
            <path d="m9 18 6-6-6-6" />
          </svg>
        </button>
      </div>
      <table
        class="bootcn-calendar__grid"
        role="grid"
        aria-labelledby="bootcn-date-range-picker-calendar-title"
      >
        <thead>
          <tr></tr>
        </thead>
        <tbody></tbody>
      </table>
      <input type="hidden" name="check-in" data-bootcn-calendar-input="start" />
      <input type="hidden" name="check-out" data-bootcn-calendar-input="end" />
    </div>
  </div>
</div>
css
.bootcn-date-picker {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  gap: 0.5rem;
}

.bootcn-date-picker__trigger {
  justify-content: flex-start;
  min-width: 15rem;
  font-weight: 400;
}

.bootcn-date-picker__trigger[data-empty] {
  color: var(--bootcn-muted-foreground);
}

.bootcn-date-picker__popover {
  padding: 0;
  border: 0;
  background: transparent;
}

.bootcn-date-picker__popover .bootcn-calendar {
  box-shadow: var(--bootcn-shadow-md);
}
js
(function () {
  'use strict';

  const ns = (window.Bootcn = window.Bootcn || {});
  if (ns.dateRangePicker) return; // already loaded: idempotent

  const ROOT = '[data-bootcn-date-range-picker]';
  let listening = false;

  function parts(root) {
    return {
      trigger: root.querySelector('[data-bs-toggle="dropdown"]'),
      text: root.querySelector('[data-bootcn-date-range-picker-text]'),
      calendar: root.querySelector('[data-bootcn-calendar]'),
    };
  }

  function toDate(value) {
    const match = /^(\d{4})-(\d{2})-(\d{2})$/.exec(value || '');
    return match ? new Date(Number(match[1]), Number(match[2]) - 1, Number(match[3])) : null;
  }

  function sync(root) {
    const { trigger, text, calendar } = parts(root);
    if (!trigger || !text || !calendar) return;
    if (!text.hasAttribute('data-placeholder'))
      text.setAttribute('data-placeholder', text.textContent);
    const start = toDate(calendar.getAttribute('data-start'));
    const end = toDate(calendar.getAttribute('data-end'));
    const owner = root.closest('[lang]');
    const format = new Intl.DateTimeFormat((owner && owner.getAttribute('lang')) || 'en', {
      dateStyle: 'medium',
    });
    let label = null;
    if (start && end && typeof format.formatRange === 'function')
      label = format.formatRange(start, end);
    else if (start && end) label = format.format(start) + ' – ' + format.format(end);
    else if (start) label = format.format(start) + ' – …';
    text.textContent = label || text.getAttribute('data-placeholder');
    trigger.toggleAttribute('data-empty', !label);
  }

  function onSelect(event) {
    const root = event.target.closest(ROOT);
    if (!root) return;
    sync(root);
    if (!event.detail || !event.detail.complete) return;
    const { trigger } = parts(root);
    if (window.bootstrap && trigger) window.bootstrap.Dropdown.getOrCreateInstance(trigger).hide();
    if (trigger) trigger.focus();
  }

  function onShown(event) {
    const root = event.target.closest && event.target.closest(ROOT);
    if (root && ns.calendar) ns.calendar.focus(parts(root).calendar);
  }

  function init(root = document) {
    if (!window.bootstrap || !window.bootstrap.Dropdown) {
      console.error(
        '[bootcn] date-range-picker: Bootstrap JavaScript is required. Load the Bootstrap bundle before bootcn date-range-picker.js.',
      );
      return;
    }
    root.querySelectorAll(ROOT).forEach(sync);
    if (listening) return;
    document.addEventListener('bootcn:calendar:select', onSelect);
    document.addEventListener('shown.bs.dropdown', onShown);
    listening = true;
  }

  function destroy() {
    if (!listening) return;
    document.removeEventListener('bootcn:calendar:select', onSelect);
    document.removeEventListener('shown.bs.dropdown', onShown);
    listening = false;
  }

  ns.dateRangePicker = { init: init, destroy: destroy };

  if (document.readyState === 'loading') {
    document.addEventListener('DOMContentLoaded', () => init());
  } else {
    init();
  }
})();

Released under the MIT License. Not affiliated with Bootstrap or shadcn/ui.