Select Multiple Dates & Date Ranges in jQuery - MultiDatesPicker

File Size: 80.8 KB
Views Total: 2878
Last Update:
Publish Date:
Official Website: Go to website
License: MIT
   
Select Multiple Dates & Date Ranges in jQuery - MultiDatesPicker

MultiDatesPicker is a jQuery plugin that extends jQuery UI Datepicker to select multiple nonconsecutive dates or a range of days from a clicked date.

It supports disabled dates, selection limits, and an alternate input field for the selected values.

Features:

  • Select or deselect individual dates in an inline or input-based calendar.
  • Preselect dates and disable unavailable days.
  • Limit the number of selected dates with maxPicks.
  • Restrict selection to a window after the first picked date with pickableRange.
  • Automatically select a group of dates relative to a clicked day.
  • Display selected dates in a separate input field.
  • Use jQuery UI Datepicker formatting, date limits, and callbacks.

How to use it:

1. Install jquery-ui-multidatespicker with npm or Yarn, or download the plugin source.

npm install jquery-ui-multidatespicker
# or
yarn add jquery-ui-multidatespicker

2. Load the jQuery UI Datepicker stylesheet, jQuery, jQuery UI, the MultiDatesPicker stylesheet, and the MultiDatesPicker script in your HTML document.

<!-- jQuery UI theme -->
<link rel="stylesheet" href="https://code.jquery.com/ui/1.14.2/themes/base/jquery-ui.css">
<link rel="stylesheet" href="jquery-ui.multidatespicker.css">

<!-- jQuery, jQuery UI, then MultiDatesPicker -->
<script src="https://code.jquery.com/jquery-3.7.1.js"></script>
<script src="https://code.jquery.com/ui/1.14.2/jquery-ui.js"></script>
<script src="jquery-ui.multidatespicker.js"></script>

3. Add an inline calendar container and an input for its selected dates.

<div id="calendar"></div>
<input type="text" id="selectedDates" readonly>

4. Initialize the multiple-date picker. altField keeps the input synchronized with the selected calendar dates.

$(function () {
  $('#calendar').multiDatesPicker({
    dateFormat: 'mm/dd/yy',
    separator: ', ',
    altField: '#selectedDates'
  });
});

Select and disable dates:

Provide arrays of dates with addDates and addDisabledDates. The example preselects two available days and blocks the day between them.

<div id="availabilityCalendar"></div>
var today = new Date();

$('#availabilityCalendar').multiDatesPicker({
  addDates: [
    today,
    new Date(today.getFullYear(), today.getMonth(), today.getDate() + 2)
  ],
  addDisabledDates: [
    new Date(today.getFullYear(), today.getMonth(), today.getDate() + 1)
  ],
  maxPicks: 5
});

Select a date range:

daysRange uses offsets from the clicked date. autoselectRange is required for this mode.

<div id="rangeCalendar"></div>
$('#rangeCalendar').multiDatesPicker({
  mode: 'daysRange',
  autoselectRange: [0, 5]
});

All Available Plugin Options:

Option Description
addDates Array of dates selected during initialization. Accepts date strings, timestamps, or JavaScript Date objects.
addDisabledDates Array of dates unavailable for selection.
separator Separator used in the selected-date string. Default: ', '.
mode 'normal' for individual dates (default), or 'daysRange' for automatic range selection.
maxPicks Maximum number of individually selected dates in normal mode.
pickableRange Number of selectable days beginning with the first selection in normal mode.
adjustRangeToDisabled Extends the pickable window to compensate for disabled dates. Default: true.
autoselectRange Two relative day offsets for 'daysRange' mode, such as [0, 5] or [-1, 2].

API Methods:

Method Description
compareDates(date1, date2) Compares two calendar dates; returns a negative, zero, or positive number.
gotDate(date, type) Returns a date's index in the picked or disabled list, or false if absent.
addDates(dates, type) Adds a date or an array of dates to the picked or disabled list.
removeDates(dates, type) Removes one or more dates by value.
removeIndexes(indexes, type) Removes one or more dates by their array indexes.
resetDates(type) Clears picked dates, or disabled dates when passed 'disabled'.
toggleDate(date, type) Adds or removes a date; in days-range mode, selects the configured relative range.
getDates(format, type) Returns selected dates as an array of strings (default), Date objects, or timestamps.
value(string) Returns selected dates as one string, or parses a supplied string and adds its dates.
destroy() Removes MultiDatesPicker and the underlying Datepicker instance.

The optional type parameter is 'picked' by default. Pass 'disabled' to inspect or modify the disabled-date list.

var $calendar = $('#calendar');
var todayString = $.datepicker.formatDate('mm/dd/yy', new Date());

// Add a date using an array or a formatted string.
$calendar.multiDatesPicker('addDates', [todayString]);

// Read selected dates as strings or Date objects.
var dates = $calendar.multiDatesPicker('getDates');
var dateObjects = $calendar.multiDatesPicker('getDates', 'object');

// Find, toggle, or remove a selected date.
var index = $calendar.multiDatesPicker('gotDate', todayString);
$calendar.multiDatesPicker('toggleDate', todayString);
$calendar.multiDatesPicker('removeDates', [todayString]);

// Clear selected dates or disabled dates.
$calendar.multiDatesPicker('resetDates');
$calendar.multiDatesPicker('resetDates', 'disabled');

// Get the comma-separated selection.
var dateString = $calendar.multiDatesPicker('value');

// Tear down the calendar when it is no longer needed.
$calendar.multiDatesPicker('destroy');

Callbacks and Change Events:

Callback When it runs
beforeShow(input, inst) Before an input-based calendar opens.
onSelect(dateText, inst) After a date is clicked.
beforeShowDay(date) For each calendar day, allowing custom availability and CSS classes.
onClose(dateText, inst) When the Datepicker popup closes.
<div id="callbackCalendar"></div>
$('#callbackCalendar').multiDatesPicker({
  onSelect: function (dateText, inst) {
    console.log($(this).multiDatesPicker('getDates'));
  },
  beforeShowDay: function (date) {
    return [date.getDay() !== 0 && date.getDay() !== 6, ''];
  }
});

The alternate input also fires a change event when its selected-date string updates.

$('#selectedDates').on('change', function () {
  console.log($(this).val());
});

Related Date Picker & Calendar Resources:

Changelog:

v1.6.9 (2025-01-15)

  • Triggers a change event when the selected value is updated.

v1.6.8 (2024-12-19)

  • Updated compatibility with jQuery and jQuery UI.

This awesome jQuery plugin is developed by dubrox. For more Advanced Usages, please check the demo page or visit the official website.