Reputation: 523
I will get the date of birth value dynamically from users visiting to the site, but when the user will insert birthday value I have to check whether the user is 18 years old or not.
user will insert a value like 18/09/2012
as dd/mm/yyyy
format.
var arr = date.split("/"); //date is the value of birth date.
var day = arr[0];
var month = arr[1];
var year = arr[2];
How should I do it?
Upvotes: 2
Views: 860
Reputation: 6222
you may try any of these functions Age Calculation from Date of Birth Using Javascript/Jquery or check this one Calculate age in JavaScript
Upvotes: 0
Reputation: 17080
Take a look at the following question, it will help you calculate the age -
Calculate age in JavaScript
function getAge(dateString)
{
var today = new Date();
var birthDate = new Date(dateString);
var age = today.getFullYear() - birthDate.getFullYear();
var m = today.getMonth() - birthDate.getMonth();
if (m < 0 || (m === 0 && today.getDate() < birthDate.getDate()))
{
age--;
}
return age;
}
Upvotes: 1