Explain Codes LogoExplain Codes Logo

Set date input field's max date to today

javascript
date-input
max-date
javascript-date
Alex KataevbyAlex Kataev·Sep 20, 2024
TLDR

Set the HTML date input to today or a prior date with this JavaScript one-liner:

document.querySelector('input[type="date"]').max = new Date().toISOString().split('T')[0];

This line sets the max attribute to the current date, formatted as YYYY-MM-DD, the standard format for date inputs.

Implementing the solution

JavaScript: Set max date simply

To dynamically limit the date range in a date input field, use the following command:

let datePicker = document.getElementById('datePickerId'); // Because Mother Nature also has a deadline. datePicker.max = new Date().toISOString().split('T')[0];

Or if you want the date in a different format:

// A French souvenir in Canada, eh? datePicker.max = new Date().toLocaleDateString('fr-ca');

SetAttribute method for max date

setAttribute can also be used to set the max date:

let formattedDate = new Date().toISOString().split('T')[0]; // Because even time travelling has its limits. inputField.setAttribute('max', formattedDate);

Managing time zones

Consider time zones to ensure global consistency. Adjust the JavaScript Date object like this:

let today = new Date(new Date().getTime() - new Date().getTimezoneOffset() * 60000).toISOString().split('T')[0]; datePicker.max = today;

PHP approach

For server-side developers, PHP can streamline the task without JavaScript:

<input type="date" max="<?= date('Y-m-d'); ?>">

Leap years and more

Leap years and other date anomalies are correctly handled in JavaScript, thanks to built-in Date object methods!

Visual representation

While dealing with date brackets, imagine restricting the end date to the present:

🏁📅: [Start Date] ----> [Today's Date] (The future is forbidden!)

On your form:

<input type="date" id="datePickerId" max="YYYY-MM-DD"> 📅
Example: 📅 Today's max: `max="2023-04-14"` (Suppose today is April 14, 2023) Final Result: 🚫 Future dates are out of bounds!

User interaction

The max attribute set this way prevents users from selecting a future date, enhancing both data integrity and user experience (UX).

Automated Updates

The input field automatically updates daily, so there's no need to manually adjust the max date.

Consistency and compatibility

The YYYY-MM-DD format ensures consistency across international date standards and also compatibility with a range of browsers.

Bonus tips and tricks

jQuery version

If you're a jQuery lover, set the max date restriction like this:

$('[type="date"]').prop('max', new Date().toJSON().split('T')[0]);

Potential pitfalls and how to dodge them

Prepare for scenarios where browser compatibility or users having JavaScript disabled may interfere. Mitigate these issues to ensure a flawless experience.

Staying up-to-date

It's vital to stay abreast of the latest web standards and browser updates to maintain cross-browser compatibility for any date input restrictions.