You are here: irt.org | FAQ | JavaScript | Date | Q29 [ previous next ]
<script language="JavaScript"><!--
function getAge(dateString,dateType) {
/*
function getAge
parameters: dateString dateType
returns: boolean
dateString is a date passed as a string in the following
formats:
type 1 : 19970529
type 2 : 970529
type 3 : 29/05/1997
type 4 : 29/05/97
dateType is a numeric integer from 1 to 4, representing
the type of dateString passed, as defined above.
Returns string containing the age in years, months and days
in the format yyy years mm months dd days.
Returns empty string if dateType is not one of the expected
values.
*/
var now = new Date();
var today = new Date(now.getYear(),now.getMonth(),now.getDate());
var yearNow = now.getYear();
var monthNow = now.getMonth();
var dateNow = now.getDate();
if (dateType == 1)
var dob = new Date(dateString.substring(0,4),
dateString.substring(4,6)-1,
dateString.substring(6,8));
else if (dateType == 2)
var dob = new Date(dateString.substring(0,2),
dateString.substring(2,4)-1,
dateString.substring(4,6));
else if (dateType == 3)
var dob = new Date(dateString.substring(6,10),
dateString.substring(3,5)-1,
dateString.substring(0,2));
else if (dateType == 4)
var dob = new Date(dateString.substring(6,8),
dateString.substring(3,5)-1,
dateString.substring(0,2));
else
return '';
var yearDob = dob.getYear();
var monthDob = dob.getMonth();
var dateDob = dob.getDate();
yearAge = yearNow - yearDob;
if (monthNow >= monthDob)
var monthAge = monthNow - monthDob;
else {
yearAge--;
var monthAge = 12 + monthNow -monthDob;
}
if (dateNow >= dateDob)
var dateAge = dateNow - dateDob;
else {
monthAge--;
var dateAge = 31 + dateNow - dateDob;
if (monthAge < 0) {
monthAge = 11;
yearAge--;
}
}
return yearAge + ' years ' + monthAge + ' months ' + dateAge + ' days';
}
document.write(getAge("19650104",1)+'<BR>');
document.write(getAge("650104",2)+'<BR>');
document.write(getAge("04/01/1965",3)+'<BR>');
document.write(getAge("04/01/65",4)+'<BR>');
//--></script>Many thanks to Stig Ã…hlberg for recommending changes to improve the reliability of the answer.
The following was submitted by Dean Maynard
The following code returns the age in years. You provide the birthdate. The code is a bit simpler and takes into account that some versions of netscape need to have 1900 added to the year.
If you were born on March 24, 1952, here's an example of how you'd call the function:
var AgeInYears = Age(3,24,1952)
That's it! Please note that the year must be passed as 4 digits - not 2!
<script language="JavaScript" type="text/javascript"><!--
function Age(monthDob,dayDob,yearDob) {
var now = new Date();
var yearNow = now.getYear();
var monthNow = now.getMonth() + 1;
var dayNow = now.getDate();
var today = new Date(yearNow,monthNow,dayNow);
if (yearNow < 100) {
yearNow=yearNow+1900;
}
yearAge = yearNow - yearDob;
if (monthNow <= monthDob) {
if (monthNow < monthDob) {
yearAge--;
} else {
if (dayNow < dayDob) {
yearAge--;
}
}
}
return yearAge
}
//--></script>