javascript获取2个日期相隔的年份得到年龄的方法
function getAge(birthDate,currentDate){ var returnAge; var strBirthdayArr=birthDate.split("-"); var birthYear = strBirthdayArr[0]; var birthMonth = strBirthdayArr[1]; var birthDay = strBirthdayArr[2]; var strCurrentDateArr=currentDate.split("-"); var nowYear = strCurrentDateArr[0]; var nowMonth = strCurrentDateArr[1]; var nowDay = strCurrentDateArr[2]; if(nowYear == birthYear) { returnAge = 0;//同年 则为0岁 } else { var ageDiff = nowYear - birthYear ; //年之差 if(ageDiff > 0) { if(nowMonth == birthMonth) { var dayDiff = nowDay - birthDay;//日之差 if(dayDiff < 0) { returnAge = ageDiff - 1; } else { returnAge = ageDiff ; } } else { var monthDiff = nowMonth - birthMonth;//月之差 if(monthDiff < 0) { returnAge = ageDiff - 1; } else { returnAge = ageDiff ; } } } else { returnAge = -1;//返回-1 表示出生日期输入错误 晚于今天 } } return returnAge;//返回周岁年龄 }