<script>
function ageCalculator() {
var userinput = document.getElementById("DOB").value;
var dob = new Date(userinput);
if(userinput==null || userinput=='') {
document.getElementById("message").innerHTML = "**Choose a date please!";
return false;
} else {
var month_diff = Date.now() - dob.getTime();
var age_dt = new Date(month_diff);
var year = age_dt.getUTCFullYear();
var age = Math.abs(year - 1970);
alert(document.getElementById("result").innerHTML =
"Age is: " + age + " years. ")
}
}
</script>
<script>
const _MS_PER_DAY = 1000 * 60 * 60 * 24;
// a and b are javascript Date objects
function dateDiffInDays(a, b) {
// Discard the time and time-zone information.
const utc1 = Date.UTC(a.getFullYear(), a.getMonth(), a.getDate());
const utc2 = Date.UTC(b.getFullYear(), b.getMonth(), b.getDate());
return Math.floor((utc2 - utc1) / _MS_PER_DAY);
}
// test it
const a = new Date("2017-01-01"),
b = new Date("2017-07-25"),
difference = dateDiffInDays(a, b);
</script>
<script>
var date1 = new Date("7/11/2010");
var date2 = new Date("8/11/2010");
var diffDays = parseInt((date2 - date1) / (1000 * 60 * 60 * 24), 10);
alert(diffDays )
</script>
</head>
<body>
<center>
<h2 style="color: 32A80F" align="center"> Calculate Age from Date of Birth <br> <br> </h2>
<b> Enter Date of Birth: <input type=date id = DOB> </b>
<span id = "message" style="color:red"> </span> <br><br>
<button type="submit" onclick = "ageCalculator()"> Calculate age </button> <br><br>
<h3 style="color:32A80F" id="result" align="center"></h3>
</center>
</body>
</html>
var lblError = document.getElementById("lblError");
//Get the date from the TextBox.
var dateinput = document.getElementById("dob").value;
var dob = new Date(dateinput);
if(dateinput==null || dateinput=='')
{
//document.getElementById("dob_error").innerHTML = "Please Enter Date of Birth";
// $("#dob_error").html("Enter Date of Birth");
return false;
}
else{
//calculate month difference from current date in time
var month_diff = Date.now() - dob.getTime();
//convert the calculated difference in date format
var age_dt = new Date(month_diff);
//extract year from date
var year = age_dt.getUTCFullYear();
//now calculate the age of the user
var age = Math.abs(year - 1970);
// alert(age);
lblError.innerHTML = "Date Of Birth is Not Eligible";
if(age >= 42)
{
var nullstr = "";
$("#dob").val(nullstr);
document.getElementById("dob").focus();
return false;
}
else if(age <= 21)
{
var nullstr = "";
$("#dob").val(nullstr);
document.getElementById("dob").focus();
return false;
}
lblError.innerHTML = "";
return true;
}
}
else{
var lblError = document.getElementById("lblError");
//Get the date from the TextBox.
var dateinput = document.getElementById("dob").value;
var dob = new Date(dateinput);
if(dateinput==null || dateinput=='')
{
//document.getElementById("dob_error").innerHTML = "Please Enter Date of Birth";
// $("#dob_error").html("Enter Date of Birth");
return false;
}
else{
//calculate month difference from current date in time
var month_diff = Date.now() - dob.getTime();
//convert the calculated difference in date format
var age_dt = new Date(month_diff);
//extract year from date
var year = age_dt.getUTCFullYear();
//now calculate the age of the user
var age = Math.abs(year - 1970);
// alert(age);
lblError.innerHTML = "Date Of Birth is Not Eligible";
if(age >= 39)
{
var nullstr = "";
$("#dob").val(nullstr);
document.getElementById("dob").focus();
return false;
}
else if(age <= 21)
{
var nullstr = "";
$("#dob").val(nullstr);
document.getElementById("dob").focus();
return false;
}
lblError.innerHTML = "";
return true;
}
}
/*
var parts = dob.split("/");
var dtDOB = new Date(parts[2] + "/" + parts[1] + "/" + parts[0]);
// alert(dtDOB);
var dtCurrent = new Date();
lblError.innerHTML = "Date Of Birth is Not Eligible";