jhunlio
jhunlio

Reputation: 2660

auto change birth date if given birth date given?

I have 2 input one is input for "Birth Date" and the one is for the Age, is there anyway, if I change the Birth Date, the Age input automatically change.

<input type="text" name="birthdate" value="07/24/1988" />
<input type="text" name="age" value="32" />

Upvotes: 0

Views: 307

Answers (1)

Shubham Dixit
Shubham Dixit

Reputation: 1

You can calculate the difference of current date and the date entered in years and then change the value of the age on keyup event like below

function age(dt) {

  var diff = ((new Date().getTime() - new Date(dt).getTime()) / 1000) / (60 * 60 * 24);

  document.getElementById("age").value = Math.abs(Math.round(diff / 365.25));


}
<input type="text" name="birthdate" value="07/24/1988" onkeyup="age(this.value)" />
<input type="text" name="age" value="32" id="age" />

Upvotes: 1

Related Questions