how to restrict bootstrap date picker from future date
Categories:
Restricting Bootstrap Datepicker to Prevent Future Date Selection

Learn how to configure the Bootstrap Datepicker to disable the selection of future dates, ensuring users can only pick today's date or past dates. This guide covers common methods and practical examples.
The Bootstrap Datepicker is a popular and versatile component for web forms, providing an intuitive way for users to select dates. However, in many applications, it's crucial to prevent users from selecting dates in the future. This is common for fields like 'Date of Birth', 'Issue Date', or 'End Date' for past events. This article will guide you through the process of configuring the Bootstrap Datepicker to restrict future date selections, ensuring data integrity and a better user experience.
Understanding the Bootstrap Datepicker endDate
Option
The Bootstrap Datepicker library provides a powerful option called endDate
that allows you to define the maximum selectable date. By setting this option dynamically to today's date, you can effectively disable all future dates. This is the most straightforward and recommended approach for this requirement.
$(document).ready(function() {
$('#datepicker').datepicker({
format: 'dd/mm/yyyy',
autoclose: true,
endDate: '0d' // '0d' means today's date
});
});
Initializing Bootstrap Datepicker with endDate: '0d'
endDate
option accepts various formats: a specific date string (e.g., '31/12/2023'), a Date object, or a string like '0d' (today), '-1d' (yesterday), '+1w' (one week from today), etc. Using '0d' is ideal for dynamically setting the end date to the current day.Implementing the Restriction with JavaScript
To implement this, you'll need to include the Bootstrap Datepicker CSS and JavaScript files, along with jQuery. Then, select your date input field using its ID or class and initialize the datepicker with the endDate
option set to '0d'
.
<!-- HTML Structure -->
<input type="text" class="form-control" id="datepicker" placeholder="Select a date">
<!-- Include jQuery (before Bootstrap and Datepicker JS) -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<!-- Include Bootstrap JS (if not already included) -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js"></script>
<!-- Include Bootstrap Datepicker JS -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datepicker/1.9.0/js/bootstrap-datepicker.min.js"></script>
<!-- Your JavaScript to initialize the datepicker -->
<script>
$(document).ready(function() {
$('#datepicker').datepicker({
format: 'dd/mm/yyyy',
autoclose: true,
endDate: '0d' // Restricts selection to today or past dates
});
});
</script>
Complete HTML and JavaScript setup for restricting future dates

Bootstrap Datepicker with future dates disabled
Handling Different Date Formats and Timezones
When working with endDate: '0d'
, the datepicker automatically uses the client's local date. If your application requires specific date formats or needs to account for server-side timezones, ensure your format
option matches your desired display, and consider server-side validation as an additional layer of security. The endDate
option handles the client-side restriction effectively.
$(document).ready(function() {
// Example with a different format
$('#datepicker-us').datepicker({
format: 'mm/dd/yyyy',
autoclose: true,
endDate: '0d'
});
// Example setting a specific date as endDate (e.g., end of current year)
var endOfYear = new Date(new Date().getFullYear(), 11, 31);
$('#datepicker-eoy').datepicker({
format: 'yyyy-mm-dd',
autoclose: true,
endDate: endOfYear
});
});
Datepicker initialization with different formats and a specific endDate
endDate: '0d'
enhance user experience, they are not a substitute for server-side validation. Always validate date inputs on the server to prevent malicious or incorrect data from being submitted.1. Include Dependencies
Ensure jQuery, Bootstrap's JavaScript, and the Bootstrap Datepicker's JavaScript and CSS files are correctly linked in your HTML document. Place jQuery before other scripts.
2. Add Input Field
Create an <input type="text">
element in your HTML where the datepicker will be attached. Assign it a unique ID, for example, id="datepicker"
.
3. Initialize Datepicker
In a <script>
block, use jQuery to select your input field and call the .datepicker()
method. Set the endDate
option to '0d'
to restrict future dates.
4. Test Functionality
Open your web page in a browser and click on the date input field. Verify that the calendar opens and all dates from tomorrow onwards are grayed out and cannot be selected.