Set HTML Date Picker Max Date to Today



In HTML date picker is a useful concept as it enables people to select a specific date on a website or even a form. By default, date pickers accept any date to be entered, however, if a user wishes to limit the range of dates that are selectable?say from the past until today.

What is a Date Picker?

A date picker is a form of user interface component that enables users to select dates through a calendar or clock. Most usually it is used in forms when a date has to be entered for example in reservation systems, scheduling software, or entering birthdates.

Steps for setting the Max Date to Today

When you want to establish a set of possible dates in an HTML date picker, you use min and max. In today's example, we will concentrate on how to set the max date to the current date.

Here's how you can set up a date picker where the maximum selectable date is the current day ?

<input type="date" id="datepicker" max="">

Combining Data with MAX Attribute to Today

For a dynamic setting of the max attribute to have today's date, one can use JavaScript to generate the current date in the format of (YYYY-MM-DD). This format is necessary for date pickers to work correctly to conform with the accessibility standard.

Here's a step-by-step guide to setting the max attribute of the date picker ?

HTML Structure

Develop an HTML forminput tag which specifies its type as a date. This makes it a date picker.

<input type="date" id="datepicker" />

JavaScript to Set the Max Date

Through JavaScript, extract today's date in the complete format and add it to the max attribute of input.

  • JavaScript code gets the current date by constructing the Date() object.
  • It first transforms the date into a string in the right format YYYY-MM-DD.
  • Last it defines the max attribute of the date picker to today to restrict the date selection beyond today
  • Restricting the maximum date to today is useful in various scenarios:
  • Other restrictions such as the use of booking systems that allow only present-day bookings.
  • Date fields where you would like to restrict the date to either past or present only, for example with Birthdays or joining events.
  • Data-collection forms that have future date fields that are not areas of interest for future or current audits.

Example

Here's the full code with JavaScript ?

<html>
<head>
<title>Date Picker</title>
</head>
<body>
<input type="date" id="datepicker" />
<script>
   const today = new Date();
   const year = today.getFullYear();
   const month = String(today.getMonth() + 1).padStart(2, '0');
   const day = String(today.getDate()).padStart(2, '0');
   const formattedDate = `${year}-${month}-${day}`;
   document.getElementById('datepicker').setAttribute('max', formattedDate);
</script>
</body>
</html>

Conclusion

In this article to set an HTML date picker max date to today, we have discussed step wise explanation of code with example. You can follow these steps for implementing the given problem.

Updated on: 2025-01-28T13:16:54+05:30

437 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements