In: Computer Science
Is there a way to tell the user in the alert how old they are based on their birthday?
<html>
<head>
</head>
<body style="background-color: lightgreen;">
<center><h1>Contact List</h1></center>
<center>
<form>
<label>firstname: </label>
<input type="text" id="firstname">
<br/><br/>
<label>lastname:</label>
<input type="text" id="lastname">
<br/><br/>
<label>birthday:</label>
<input type="date" id="birthday">
<br/><br/>
<label>address:</label>
<textarea id="addr"></textarea>
<br/><br/>
<input type=submit onclick="datacalc()">
</form>
</center>
<script src="script.js">
</script>
</body>
</html>
//function to call when form is submitted
function datacalc(){
//creating and storing the form data into the person object
var person = {
//1. storing each value in respective data components
firstname : document.getElementById('firstname').value,
lastname : document.getElementById('lastname').value,
//3. Storing the date value in birth propertyName
birth : document.getElementById('birthday').value,
addr : document.getElementById('addr').value,
//2. A method to return greetings to the user
greet : function() {
return "Hi "+this.firstname+"... Glad to meet you !\n";
}
};
//calling the greet function and accessing object values of person
alert( person.greet() + "Your birthday is on " + person.birth );
localStorage.setItem('p', JSON.stringify(person)); //to store the object after serializing it. 'p' is just the id to retrieve it back later. stringify() will convert it into serialized form.
var newPerson = JSON.parse(localStorage.getItem('p')); //to retrieve back the serialized object we stored using the id 'p' we assigned to it. parse() will convert it back into the original object.
alert("Your name as stored in storage is " + newPerson.firstname + " " + newPerson.lastname + " and your birth date stored is " + newPerson.birth);
}
I have written there my contribution where I have edited the code.
<html>
<head>
<meta content="text/html;charset=utf-8" http-equiv="Content-Type">
<meta content="utf-8" http-equiv="encoding">
</head>
<body style="background-color: lightgreen;">
<center><h1>Contact List</h1></center>
<center>
<form>
<label>firstname: </label>
<input type="text" id="firstname">
<br/><br/>
<label>lastname:</label>
<input type="text" id="lastname">
<br/><br/>
<label>birthday:</label>
<input type="date" id="birthday">
<br/><br/>
<label>address:</label>
<textarea id="addr"></textarea>
<br/><br/>
<input type=submit onclick="datacalc()">
</form>
</center>
<script>
function datacalc(){
//creating and storing the form data into the person object
var person = {
firstname : document.getElementById('firstname').value,
lastname : document.getElementById('lastname').value,
//3. Storing the date value in birth propertyName
birth : document.getElementById('birthday').value,
addr : document.getElementById('addr').value,
//2. A method to return greetings to the user
greet : function() {
return "Hi "+this.firstname+"... Glad to meet you !\n";
}
};
//calling the greet function and accessing object values of person
var today = new Date();
var dd = String(today.getDate()).padStart(2, '0');
var mm = String(today.getMonth() + 1).padStart(2, '0'); //January is 0!
var yyyy = today.getFullYear();
today = yyyy + '-' + mm + '-'+dd;
localStorage.setItem('p', JSON.stringify(person)); //to store the object after serializing it. 'p' is just the id to retrieve it back later. stringify() will convert it into serialized form.
var newPerson = JSON.parse(localStorage.getItem('p')); //to retrieve back the serialized object we stored using the id 'p' we assigned to it. parse() will convert it back into the original object.
// My Contribution needed a function to calculte the date
function diff_year_month_day(dt1, dt2)
{
var time =(dt2.getTime() - dt1.getTime()) / 1000;
var year = Math.abs(Math.round((time/(60 * 60 * 24))/365.25));
var month = Math.abs(Math.round(time/(60 * 60 * 24 * 7 * 4)));
var days = Math.abs(Math.round(time/(3600 * 24)));
return( "Year :- " + year + " Month :- " + month%12 + " Days :-" + days%31);
}
var today = new Date();
var date = today.getFullYear()+'-'+(today.getMonth()+1)+'-'+today.getDate();
dt1 = new Date(date);
dt2 = new Date(newPerson.birth);
alert("Your name as stored in storage is " + newPerson.firstname + " " + newPerson.lastname + " and your birth date stored is " + newPerson.birth+ "\n Age of Person is "+ diff_year_month_day(dt2, dt1));
}
</script>
</body>
</html>