有 2 个 javascript 日期,
第一个是生日
第二个是从该日期计算年龄的日期。
最好的方法应该是什么?
function calculateAge (birthDate, otherDate) {
birthDate = new Date(birthDate);
otherDate = new Date(otherDate);
var years = (otherDate.getFullYear() - birthDate.getFullYear());
if (otherDate.getMonth() < birthDate.getMonth() ||
otherDate.getMonth() == birthDate.getMonth() && otherDate.getDate() < birthDate.getDate()) {
years--;
}
return years;
}
Example:
var age = calculateAge("02/24/1991", "02/24/2010"); // Format: MM/DD/YYYY
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)