-2

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?

Pizzaman
  • 365
  • 4
  • 8
  • 19

1 Answers1

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
  • 14,618
  • 18
  • 81
  • 134