I need to calculate the date of birth of a person based on current day when giving the input as current age in java script. I searched it but not getting. I need to get the persons date of birth according to current date time.
Any idea??
I need to calculate the date of birth of a person based on current day when giving the input as current age in java script. I searched it but not getting. I need to get the persons date of birth according to current date time.
Any idea??
If the current year is 2016 and they say they are 16 years old, they could have been born in 1999 or 2000 depending on whether their birthday already passed. You can't really get the day/month. If you ask whether their birthday is already passed this year, you can get the right year
document.getElementById('calculate').addEventListener('click', function(e){
var today = new Date();
var currentYear = today.getFullYear() ;
var age = parseInt(document.getElementById('age').value, 10);
var birthdayPast = document.getElementById('past').checked;
var birthYear = currentYear - age - (birthdayPast ? 0 : 1);
document.getElementById('result').innerHTML =
"You were born in " + birthYear;
});
<label>Age: <input id='age' value='16'/></label>
<label>Birthday past?<input type='checkbox' id='past'/></label>
<button id='calculate'>Find birth year</button>
<div id='result'>
</div>