When a user enters their date of birth in a form as DDMMYYYY, how can I grab that and convert it into years to tell them their age?
Asked
Active
Viewed 607 times
1 Answers
2
You need to work with the Date
object to get the age:
var dateOfBirth = new Date(2010, 6, 17);
var today = new Date();
var diff = new Date(today.getTime() - dateOfBirth.getTime());
alert("Age: " + Number(diff.getUTCFullYear() - 1970) + " years, " + diff.getUTCMonth() + " months, " + Number(diff.getUTCDate() - 1) + " days");
Readup: Date
| MDN

Rahul Desai
- 15,242
- 19
- 83
- 138