微信小程序根据生日获取年龄
// 根据出生日期计算年龄周岁 传参格式为1996-06-08
// 根据出生日期计算年龄周岁 传参格式为1996-06-08 function getAge(strBirthday) { var returnAge = ''; var mouthAge = ''; var strBirthdayArr = strBirthday.split("-"); var birthYear = strBirthdayArr[0]; var birthMonth = strBirthdayArr[1]; var birthDay = strBirthdayArr[2]; var d = new Date(); var nowYear = d.getFullYear(); var nowMonth = d.getMonth() + 1; var nowDay = d.getDate(); if (nowYear == birthYear) { // returnAge = 0; //同年 则为0岁 var monthDiff = nowMonth - birthMonth; //月之差 if (monthDiff < 0) { } else { mouthAge = monthDiff + '个月'; } } 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 { mouthAge = monthDiff + '个月'; returnAge = ageDiff + '岁'; } } } else { returnAge = -1; //返回-1 表示出生日期输入错误 晚于今天 } } return returnAge + mouthAge; //返回周岁年龄+月份 }
// 根据出生日期计算年龄周岁 传参格式为19960608
// 根据出生日期计算年龄周岁 传参格式为19960608 function getAges(strBirthday) { var returnAge = ''; var mouthAge = ''; var strBirthdayArr = strBirthday var birthYear = strBirthdayArr.substring(0,4); var birthMonth = strBirthdayArr.substring(4, 6); var birthDay = strBirthdayArr.substring(6, 8); var d = new Date(); var nowYear = d.getFullYear(); var nowMonth = d.getMonth() + 1; var nowDay = d.getDate(); if (nowYear == birthYear) { // returnAge = 0; //同年 则为0岁 var monthDiff = nowMonth - birthMonth; //月之差 if (monthDiff < 0) { } else { mouthAge = monthDiff + '个月'; } } 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 { mouthAge = monthDiff + '个月'; returnAge = ageDiff + '岁'; } } } else { returnAge = -1; //返回-1 表示出生日期输入错误 晚于今天 } } //return returnAge + mouthAge; //返回周岁年龄+月份 return returnAge }
作者:子钦加油
出处:https://www.cnblogs.com/zmdComeOn/
个性签名:努力生活,努力走路
阿里云拼团:https://www.aliyun.com/1111/home?userCode=f4ee1llo1核2G1M,86一年,229三年;2核4G5M,799三年;2核8G5M,1399三年
腾讯云三月采购计划特价:https://cloud.tencent.com/act/cps/redirect?redirect=1073&cps_key=15d0b1673287c43fe946626d9f4e2eee&from=console1核2G1M,88一年;1核2G1M,268三年;2核4G5M,998一年;4核8G5M,2888元三年
出处:https://www.cnblogs.com/zmdComeOn/
个性签名:努力生活,努力走路
阿里云拼团:https://www.aliyun.com/1111/home?userCode=f4ee1llo1核2G1M,86一年,229三年;2核4G5M,799三年;2核8G5M,1399三年
腾讯云三月采购计划特价:https://cloud.tencent.com/act/cps/redirect?redirect=1073&cps_key=15d0b1673287c43fe946626d9f4e2eee&from=console1核2G1M,88一年;1核2G1M,268三年;2核4G5M,998一年;4核8G5M,2888元三年