Mon. Aug 26th, 2019

Codes

Codes for Coders

Javascript JQuery Age calculator by Arvind Kumar Singh

1 min read

Javascript JQuery Age Calculator by Arvind kumar Singh, In this blog I will tell you how to how to calculate age for usingd jQuery and java-script.

 

<!DOCTYPE html>
<html>
<head>
<title>Age Calculator</title>

<link rel=”stylesheet” href=”//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css”>
<script src=”https://code.jquery.com/jquery-1.12.4.js”></script>
<script src=”https://code.jquery.com/ui/1.12.1/jquery-ui.js”></script>

<script type=”text/javascript”>
$( function() {
$( “.mydate” ).datepicker({
dateFormat: ‘yy-mm-dd’,
changeMonth: true,
changeYear: true,
yearRange: ‘1970:’+(new Date).getFullYear(),

});
});

</script>

</head>
<body>
<h1><cenrter>Age Calculator</cenrter></h1>
Date Of Birth:
<input type=”text”  class=”mydate”  id=”birth_date” onChange=”ageCalculate()”>

<br/><br/>
Age:
<br/>
<br/>
<div id=”adge”></div>
<input type=”text” id=”age”>

<script>
function ageCalculate(){
var birthDate =document.getElementById(‘birth_date’).value;
var d = new Date(birthDate);

var mdate = birthDate.toString();
var yearThen = parseInt(mdate.substring(0,4), 10);
var monthThen = parseInt(mdate.substring(5,7), 10);
var dayThen = parseInt(mdate.substring(8,10), 10);

var today = new Date();
var birthday = new Date(yearThen, monthThen-1, dayThen);
var differenceInMilisecond = today.valueOf() – birthday.valueOf();

var year_age = Math.floor(differenceInMilisecond / 31536000000);
var day_age = Math.floor((differenceInMilisecond % 31536000000) / 86400000);

if ((today.getMonth() == birthday.getMonth()) && (today.getDate() == birthday.getDate())) {
alert(“Happy B’day!!!”);        }

var month_age = Math.floor(day_age/30);

day_age = day_age % 30;

var tMnt= (month_age + (year_age*12));
var tDays =(tMnt*30) + day_age;

if (isNaN(year_age) || isNaN(month_age) || isNaN(day_age)) {
document.getElementById(“age”).innerHTML = (“Invalid birthday – Please try again!”);
}
else {
$(‘#age’).val(year_age + ” years ” + month_age + ” months ” + day_age + ” days”);

}

}
</script>
</body>
</html>

Leave a Reply

Your email address will not be published. Required fields are marked *