Configuring and Implementing Date Range Restrictions in Bootstrap Datepicker

Nov 30, 2025 · Programming · 15 views · 7.8

Keywords: Bootstrap Datepicker | Date Range Restrictions | startDate | endDate | JavaScript Configuration

Abstract: This article provides an in-depth exploration of how to configure and implement date range restrictions in Bootstrap Datepicker. By analyzing the usage of startDate and endDate options with concrete code examples, it demonstrates how to set both relative and absolute date ranges. The article also covers advanced techniques for dynamically adjusting date ranges, including the use of changeDate events and setStartDate/setEndDate methods, helping developers create more flexible and user-friendly date selection interfaces.

Basic Configuration of Date Range Restrictions

Bootstrap Datepicker offers robust date range restriction capabilities primarily through the startDate and endDate options. Similar to jQuery UI's minDate and maxDate, these options allow developers to define the minimum and maximum selectable dates for users.

When configuring date ranges, multiple parameter formats are accepted:

Implementation of Relative Date Ranges

Relative date range configuration is the most commonly used approach in practical development, as it dynamically calculates allowable date ranges based on the current date. Here's a typical configuration example:

$('#datepicker').datepicker({
    startDate: '-2m',
    endDate: '+2d'
});

In this example, startDate: '-2m' allows selection of dates starting from two months before the current date, while endDate: '+2d' permits selection up to two days after the current date. Relative time expressions support multiple units:

Additionally, the system provides convenient aliases: yesterday (equivalent to "-1d"), today (equivalent to "+0d"), and tomorrow (equivalent to "+1d").

Advanced Date Range Control

For more complex scenarios, such as interdependent date pickers, dynamic range adjustments can be achieved through event listeners and method calls. Here's a complete example implementing mutual constraints between start and end dates:

$(".from_date").datepicker({
    format: 'yyyy-mm-dd',
    autoclose: true
}).on('changeDate', function (selected) {
    var startDate = new Date(selected.date.valueOf());
    $('.to_date').datepicker('setStartDate', startDate);
}).on('clearDate', function (selected) {
    $('.to_date').datepicker('setStartDate', null);
});

$(".to_date").datepicker({
    format: 'yyyy-mm-dd',
    autoclose: true
}).on('changeDate', function (selected) {
    var endDate = new Date(selected.date.valueOf());
    $('.from_date').datepicker('setEndDate', endDate);
}).on('clearDate', function (selected) {
    $('.from_date').datepicker('setEndDate', null);
});

In this implementation:

Data Attribute Configuration

In addition to JavaScript configuration, Bootstrap Datepicker supports configuration through HTML data attributes. Option names are converted to corresponding data attribute formats, for example startDate becomes data-date-start-date and endDate becomes data-date-end-date.

Here's an example of configuring date ranges via data attributes:

<input type="text" class="form-control" data-date-start-date="-2m" data-date-end-date="+2d">

This approach provides a more concise configuration method, particularly useful when rendering pages server-side.

Other Related Configuration Options

Beyond basic date range restrictions, Bootstrap Datepicker offers additional configuration options to enhance the date selection experience:

The combined use of these options enables the creation of highly customized date selection interfaces that meet various complex business requirements.

Copyright Notice: All rights in this article are reserved by the operators of DevGex. Reasonable sharing and citation are welcome; any reproduction, excerpting, or re-publication without prior permission is prohibited.