Calculate Age in Javascript From Date of Birth

Javascript Code to Calculate Age from date of Birth


Here calculate_Age_From_Date_of_Birth function has two parameters there are original date of birth and final date. These two data should be YYYY-MM-DD format.

function calculate_Age_From_Date_of_Birth(originalDate, dateToCalculateAgeOn) {
if(!(typeof dateToCalculateAgeOn != 'undefined' && dateToCalculateAgeOn)) {
dateToday = new Date();
dateToCalculateAgeOn = dateToday.getFullYear() + '-' + (dateToday.getMonth() + 1) + '-' + dateToday.getDate();
}
originalDate = originalDate.replace(/[^0-9\-]/g, '').split('-');
dateToCalculateAgeOn = dateToCalculateAgeOn.replace(/[^0-9\-]/g, '').split('-');
ageDetermined = (dateToCalculateAgeOn[0] - originalDate[0]) - 1;
if(dateToCalculateAgeOn[1]>originalDate[1]||(dateToCalculateAgeOn[1]==originalDate[1]&&dateToCalculateAgeOn[2]>=originalDate[2]))
ageDetermined++;
return ageDetermined; // returns age in years
}


Related Topic Javascript to Calculate Age from Birthdate
Javascript to Calculate Date Difference
PHP to Calculate Age from Date
Javascript Date Object Constructor with Examples
How to get the HTTP Cookie Value

nScraps.com 2011   Privacy Policy  Terms of Service  Feedback