How to set date input field’s max date to today with JavaScript?

Spread the love

To set date input field’s max date to today with JavaScript, we set the max property of the date input.

For instance, we write

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

to add the date input.

Then we write

const datePickerId = document.getElementById("datePickerId");
datePickerId.max = new Date().toLocaleDateString("en-ca");

to select the date picker with getElementById.

And then we set its max property to the date string returned by toLocaleString for today’s date and time that we get with the Date constructor to set the input’s max date to today.

Leave a Reply

Your email address will not be published. Required fields are marked *