Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set date input field's max date to today

Tags:

html

I just have a simple line of code like this:

<input type='date' min='1899-01-01' max='2000-01-01'></input> 

Is there a simple way to set the max date to "today" instead of 2000-01-01? Or do I have to use Javascript to do this?

like image 753
Jack Johnson Avatar asked Sep 03 '15 14:09

Jack Johnson


People also ask

How do you set a range in input type date?

To set and get the input type date in dd-mm-yyyy format we will use <input> type attribute. The <input> type attribute is used to define a date picker or control field. In this attribute, you can set the range from which day-month-year to which day-month-year date can be selected from.

Which html5 attributes can be used with html5 date input type to limit date selection?

The max attribute specifies the maximum value (date) for a date field. Tip: Use the max attribute together with the min attribute to create a range of legal values. Tip: To set or return the value of the min attribute, use the min property.


1 Answers

You will need Javascript to do this:

HTML

<input id="datefield" type='date' min='1899-01-01' max='2000-13-13'></input> 

JS

var today = new Date(); var dd = today.getDate(); var mm = today.getMonth() + 1; //January is 0! var yyyy = today.getFullYear();  if (dd < 10) {    dd = '0' + dd; }  if (mm < 10) {    mm = '0' + mm; }       today = yyyy + '-' + mm + '-' + dd; document.getElementById("datefield").setAttribute("max", today); 

JSFiddle demo

like image 133
Shrinivas Pai Avatar answered Oct 20 '22 02:10

Shrinivas Pai