所需材料出生年、月、日 【格式】:(1879-04-23)现在的时间【格式】:(2019-1-29)方法将以上的材料转换为时间戳,在用现在的时间戳减去,出生年、月、日的时间戳,最后将所得时间戳转换为年龄代码var oneTime = ‘1879-04-23’ //出生年、月、日var twoTime = new Date() //现在的时间var BirthdayTime = getTime(oneTime) //将出生年、月、日转换为时间戳var NowTime = twoTime.getTime() //将现在的时间转换为时间戳var Time =Math.ceil(( NowTime - BirthdayTime)/31536000000) //用现在的时间戳减去出生的时间戳在除以一年的时间戳console.log(Time)注:Math.ceil为向下取整function getAge(birthday){//出生时间 毫秒var birthDayTime = new Date(birthday).getTime(); //当前时间 毫秒var nowTime = new Date().getTime(); //一年毫秒数(365 * 86400000 = 31536000000)return Math.ceil((nowTime-birthDayTime)/31536000000);}