Anidh Singh Anidh Singh - 8 months ago 87
Javascript Question

Comparing input type date with current date

<input type="date" name="bday" id="biday" required>


I have to take the date from input type date field from the user and if the entered date is greater than the current date i have to print some error in the alert box. I Have tried various methods but none of them worked for input type date. I tried to take two variables one which stores the current date and the second which takes the value of date input element `

var startDate = Date(document.getElementByID('biday').value);
var today = new Date();
if (startDate > today) {
alert("The first date is after the second date!");


`.Can someone please help me the procedure to get the task done.

Answer Source

There are number of errors on your code like getElementByID should be getElementById and you are not taking the value from input and so on. Check below snippet for reference.

function checkDate() {

  var startDate = new Date(document.getElementById('biday').value);
  var today = new Date();
  if (startDate.getTime() > today.getTime()) {
    alert("The first date is after the second date!");
  }  
}
<input type="date" name="bday" id="biday" required>
<input type="submit" value="check" onclick="checkDate()">

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download