-1

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??

san797
  • 91
  • 1
  • 9
  • 6
    There is no way to know even what year someone was born just based on their age, let alone day and month. – ach Mar 31 '16 at 18:32
  • 1
    It seems like the reverse of that link. The known value here is the age of the person. The unknown value is the DOB, which is what this Q is trying to figure out. That link has a known DOB & wants to figure out how many years old that the person is. – Clomp Mar 31 '16 at 18:35
  • 1
    yes @Clomp. You are right. – san797 Mar 31 '16 at 18:37

1 Answers1

4

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>
Ruan Mendes
  • 90,375
  • 31
  • 153
  • 217