How to set minimum and maximum date in HTML date picker
Last Updated :
18 Mar, 2024
In HTML, validating and setting the minimum and maximum dates can be done using various approaches. This is useful in the real-time applications of booking, event scheduling, etc.
Using min and max Attributes
In this approach, we are using the min and max attributes directly on the <input type="date"> element to define the acceptable range of dates. When a date is selected, an event listener triggers an alert displaying the selected date.
Syntax:
<input type="date" id="date" name="date"
min="min_date" max="max_date">
Example: The below example uses min and max attributes to set minimum and maximum dates in the HTML date picker.
HTML
<!DOCTYPE html>
<html>
<head>
<title>Example 1</title>
<style>
body {
text-align: center;
font-family: Arial, sans-serif;
}
h1 {
color: green;
}
h3 {
margin-bottom: 20px;
}
p {
margin-top: 20px;
font-size: 18px;
}
</style>
</head>
<body>
<h3>
Using min and max Attributes
</h3>
<label for="date">
Select a date:
</label>
<input type="date" id="date" name="date"
min="2024-03-15" max="2024-03-31">
<p>
Accepted Range:
2024-03-15 to 2024-03-31
</p>
<script>
const datePicker =
document.getElementById('date');
datePicker.addEventListener('change',
function () {
const selectedDate = datePicker.value;
alert('Selected Date: ' + selectedDate);
});
</script>
</body>
</html>
Output:

Using JavaScript to Set min and max Dates
In this approach, JavaScript is used to dynamically set the minimum and maximum dates for a date picker element. Upon selecting a date, the chosen date is displayed, along with the predefined date range. This enables the user to select dates within the specified range.
Syntax:
dp.setAttribute('min', '2024-03-15');
dp.setAttribute('max', '2024-03-31');
Example: The below example uses JavaScript to set min and max dates to set minimum and maximum dates in the HTML date picker.
HTML
<!DOCTYPE html>
<html>
<head>
<title>Example 2</title>
<style>
body {
text-align: center;
font-family: Arial, sans-serif;
}
h3 {
margin-bottom: 20px;
}
</style>
</head>
<body>
<h3>
Using JavaScript to Set
min and max Dates
</h3>
<label for="date">
Select a date:
</label>
<input type="date" id="date"
name="date">
<p id="selectedDate"></p>
<p id="dateRange"></p>
<script>
const dp = document.getElementById('date');
dp.setAttribute('min', '2024-03-15');
dp.setAttribute('max', '2024-03-31');
const minDate = dp.getAttribute('min');
const maxDate = dp.getAttribute('max');
document.getElementById('dateRange').textContent =
'Accepted Date Range: ' + minDate + ' to ' + maxDate;
dp.addEventListener('change', function () {
const dateSelect = dp.value;
document.getElementById('selectedDate').textContent =
'Selected Date: ' + dateSelect;
});
</script>
</body>
</html>
Output:

Similar Reads
How to Set a Minimum and Maximum Value for an input element in HTML5? To set minimum and maximum values for an input element in HTML5, we utilize the min and max attributes. For numerical inputs like type numbers, assign the desired limits directly. Alternatively, use JavaScript validation, pattern attributes, or event listeners for more complex validation scenarios,
3 min read
How to add date and time picker using only one tag in HTML ? The date picker in HTML is a convenient tool that enhances user experience by allowing users to select a date from a dropdown menu rather than typing it manually. By using the <input> element with the type attribute set to "date", you can generate an interactive date picker in your HTML docume
2 min read
How to add a date picker in form using HTML ? To add a date picker in a form using HTML, you can use the <input> element with the type attribute set to "date". A date picker is an interactive dropdown that makes it easy to choose the date from a calendar instead of typing it manually. In this article, we will learn how to add a date picke
2 min read
How to set Placeholder Value for Input Type Date in HTML? To set a placeholder value for the input type date in HTML5, use the placeholder attribute. The placeholder attribute doesn't work well with input type "date." Use onfocus="(this.type='date')" for a custom placeholder, enabling a date selection dropdown.Using OnfocusThe On-focus approach changes the
2 min read
How to Set input type date in dd-mm-yyyy format using HTML? In HTML, you can use the <input> element with the type date to allow users to pick a date. However, the format used for displaying the date may vary depending on the browser and the user's locale settings. By default, the browser uses the ISO format (yyyy-mm-dd), which is not the same as dd-mm
2 min read
HTML | DOM Input Date min Property The Input Date min property is used for setting or returning the value of the min attribute of a date field. The attribute returns a string that represents the minimum date allowed. Syntax: For returning the min property:inputdateObject.minFor setting the min property:inputdateObject.min = YYYY-MM-D
2 min read