js通过获取到的身份证号转为年龄显示

说明:现在大多都是二代身份证了,18位的,所以就没有去考虑一代身份证15位的情况。
二代身份证中,7、8、9、10位是年份,11、12位是月份,13、14位是具体的日子

代码实现:
// idNumber 身份证号 const getNowAge = (idNumber)=>{ // 根据身份证号提取出年 月 日 let yearBirthday = record.idNumber.substring(6,10) let monthBirthday = record.idNumber.substring(10,12) let dayBirthday = record.idNumber.substring(12,14) // 获取当前日期的年 月 日 let year = myDate.getFullYear() let month = myDate.getMonth() +1 let day = myDate.getDate() // 计算年龄 let nowAge = year - yearBirthday // 需要根据月份和具体日子判断下 if((monthBirthday > month) || monthBirthday == month && dayBirthday > day ){ nowAge-- } return nowAge }

posted @ 2022-07-13 14:03  正在长头发  阅读(381)  评论(0)    收藏  举报