【发布时间】:2015-03-19 05:26:27
【问题描述】:
我想在使用 jquery 日期选择器选择日期时计算年龄。我在下面添加了代码,但如果我选择像 '19/03/2015'、'15/01/2015' 或 '19/03/2014' 、'31/12/2014' 这样的日期,它会显示负值
$(document).ready(function ()
{
console.log($(document).width());
$('#patientDob').datepicker
({
dateFormat: 'dd/mm/yy',
changeMonth: true,
changeYear: true,
yearRange: '1900:2150',
maxDate: new Date(),
inline: true,
onSelect: function() {
var birthDay = document.getElementById("patientDob").value;
var DOB = new Date(birthDay);
var today = new Date();
var age = today.getTime() - DOB.getTime();
age = Math.floor(age / (1000 * 60 * 60 * 24 * 365.25));
document.getElementById('patientAge').value = age;
}
});
});
【问题讨论】:
-
“显示负值”是什么意思?
-
我严重怀疑唯一的年龄问题是负值。例如 new Date("31/12/2014") = 12 Jul 2016 ...阅读答案(已编辑,上次有问题):)。
-
我的荣幸。你也可以接受答案:)。
标签: javascript jquery html datepicker