7

Is there a way to disable dates before today in HTML5?

<input type="date">

I tried this

<input type="date" min="<?php echo $today; ?>"> 

but this works only on desktop browsers... safari mobile still allow dates prior to today.

4
  • output of $today? Commented Jul 28, 2016 at 13:51
  • The input type date is not integrate by all browser. I think you must use a JS library. caniuse.com/#feat=input-datetime Commented Jul 28, 2016 at 13:52
  • @sebastianbrosch the output of $today is 2016-07-28 Commented Jul 28, 2016 at 14:48
  • Does this answer your question? Set date input field's max date to today Commented May 6, 2021 at 19:15

4 Answers 4

15

Input date must be in ISO format (which is supported by mobile browsers).

There is no possible way to do this with pure HTML5.

But with Javascript, you can do something like:

<input name="setTodaysDate" type="date">

and;

var today = new Date().toISOString().split('T')[0];
document.getElementsByName("setTodaysDate")[0].setAttribute('min', today);

This little script will change the min with today's date in ISO format.

Live example here: jsFiddle

Sign up to request clarification or add additional context in comments.

Comments

5

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);
<input id="datefield" type='date' min='1899-01-01' max='2000-13-13'></input>

2 Comments

No need for almost all of this code: just use var today = new Date().toISOString().slice(0,10);
@HereticMonkey you have a bit of a problem with ISO cause it might get the next day - it depends on your timezone ..
0

You can try this:

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

Comments

0

if you are using jQuery try this. First add id to your element

<input type="date" id="minDate">

then in between script tag put below code

$(document).ready(function () {
    var today = new Date().toISOString().split('T')[0];
    $("#minDate").attr('min', today);
});

It should work fine

$(document).ready(function() {
  var today = new Date().toISOString().split('T')[0];
  $("#minDate").attr('min', today);
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type="date" id="minDate">

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.