DEV Community

Cover image for Set Max date to Today's date

Set Max date to Today's date

Visakh Vijayan
Willing to learn anything and everything exciting. Always awake. Skips food for fun.
・Updated on ・1 min read

Client requested for the maximum date to be selected in the date field will be of today. So we did this -

var today = new Date();

var dd = String(today.getDate()).padStart(2, '0');
var mm = String(today.getMonth() + 1).padStart(2, '0');
var yyyy = today.getFullYear();

today = yyyy + '-' + mm + '-' + dd;

<input max="` + today + `" />
Enter fullscreen mode Exit fullscreen mode

It worked for us!

Discussion (1)

hughsheehy profile image

I'd tried something similar to that. Didn't work. Then searched around StackOverflow and found all sorts of complicated solutions. Then found this. Still can't get it to work. I must be doing something particularly silly. And wrong. 🤔