// 计算年龄,返回X岁X月X日// 输入参数1.出生日期,2.当前日期function calAge(birthday){var strDate1 = birthday + " 00:00:00.0";var strDate2 = new Date().format('Y-m-d') + " 00:00:00.0";strDate1=strDate1.substring(0,strDate1.lastIndexOf(".")).replace(/-/g, "/ ");strDate2=strDate2.substring(0,strDate2.lastIndexOf(".")).replace(/-/g, "/ ");//去掉毫秒 把-替换成/ 如果不替换转成时间戳类型火狐会出问题var date1 = Date.parse(strDate1);var date2 = Date.parse(strDate2);var day = Math.ceil((date2-date1)/(60*60*1000*24));var age = '';// 真实年龄var year = Math.floor(day/365);var y = day%365;var month = Math.floor(y/30);var d = Math.floor(day%365%30);if(year > 0){age += year + '岁';}if(month > 0){age += month + '月';}if(d>0){age += d+'天';}return age;} 注:目前每月以固定30天计算,优化后续加入
Math.ceil();// 进位,有小数加1
Math.round();// 四舍五入
Math.floor(); // 退位
查询谷 - www.chaxungu.com
js 获取人员岁数(x岁x月x天)
编辑:chaxungu时间:2022-10-10 23:23:30分类:web开发
js 获取人员年龄(x岁x月x天)
上一篇:统计人数有关问题