实现方法如下:
/**
* @param strBirthday:指的是出生日期,格式为"1990-01-01"
*/
function GetAge(strBirthday){
var returnAge,
strBirthdayArr=strBirthday.split("-"),
birthYear = strBirthdayArr[0],
birthMonth = strBirthdayArr[1],
birthDay = strBirthdayArr[2],
d = new Date(),
nowYear = d.getFullYear(),
nowMonth = d.getMonth() + 1,
nowDay = d.getDate();
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;//返回周岁年龄
}
如果觉得《js生日计算年龄_js方法实现通过出生日期获取周岁年龄》对你有帮助,请点赞、收藏,并留下你的观点哦!