How can I calculate an age in years, given a birth date of format YYYYMMDD?(给定出生日期格式为YYYYMMDD,我如何计算以岁为单位的年龄?)
Is it possible using theDate()
function?(是否可以使用Date()
函数?)
I am looking for a better solution than the one I am using now:(我正在寻找比现在使用的解决方案更好的解决方案:)
var dob = '19800810'; var year = Number(dob.substr(0, 4)); var month = Number(dob.substr(4, 2)) - 1; var day = Number(dob.substr(6, 2)); var today = new Date(); var age = today.getFullYear() - year; if (today.getMonth() < month || (today.getMonth() == month && today.getDate() < day)) { age--; } alert(age);
ask by Francisc translate from so