Skip to content

Commit

Permalink
Created Age Calculator
Browse files Browse the repository at this point in the history
Created Age Calculator.
You enter the date of birth and it tell you how many years are you.
I hope you like this program.
Please if it is okay for you, could you put the hacktoberfest or hacktoberfest accepted label?
Thank you so much for the opportunity to contribute!!
  • Loading branch information
MercheLorenzo authored Oct 1, 2022
1 parent d6ab5ff commit a89351d
Showing 1 changed file with 44 additions and 0 deletions.
44 changes: 44 additions & 0 deletions Js-Projects/age-calculator/age_calculator.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<html>
<head>
<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 {

//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);

//display the calculated age
return document.getElementById("result").innerHTML =
"Age is: " + age + " years. ";
}
}
</script>
</head>
<body>
<center>
<h2 style="color: 32A80F" align="center"> Calculate Age from Date of Birth <br> <br> </h2>

<!-- Choose a date and enter in input field -->
<b> Enter Date of Birth: <input type=date id = DOB> </b>
<span id = "message" style="color:red"> </span> <br><br>

<!-- Choose a date and enter in input field -->
<button type="submit" onclick = "ageCalculator()"> Calculate age </button> <br><br>
<h3 style="color:32A80F" id="result" align="center"></h3>
</center>
</body>
</html>

0 comments on commit a89351d

Please sign in to comment.