我想从出生之日起就知道一个人的年龄。我有一个HTML代码,我正在使用dob的datepicker,当我给出生日期时,它显示自动显示年龄而不提交。请帮我找到解决方案。
<html>
<head>
<title>
DOB calculations
</title>
<link rel='stylesheet' type='text/css' href='jquery-ui.css' />
<script type='text/javascript' src='jquery.min.js'>
</script>
<script type='text/javascript' src='jquery-ui.min.js'>
</script>
<script type='text/javascript' src='jquery.js'>
</script>
<script type='text/javascript' src='jquery-ui-custom.js'>
</script>
<script type='text/javascript' src='jquery.ui.datepicker.js'>
</script>
<script type="text/javascript">
$(function() {
$('#datepicker').datepicker({
changeMonth: true,
changeYear: true,
dateFormat: 'mm/dd/yy',
//firstDay: 1,
onSelect: function(dateText, inst) {
dateText.split('/');
Bdate = new Date(dateText[2], dateText[0] - 1, dateText[1]);
BDateArr = ('' + Bdate).split(' ');
//document.getElementById('DOW').value = BDateArr[0];
Cdate = new Date;
CDateArr = ('' + Cdate).split(" ");
Age = CDateArr[3] - BDateArr[3];
document.getElementById('AGE').value = Age;
// DOBcalc(dateText);
//DOBcalc();
}
})
});
</script>
</head>
<body>
<form>
DOB (mm/dd/yyyy):
<input type="text" id="datepicker" value=''>
Age:
<input id="AGE" type="text" value="">
</form>
</body>
</html>
答案 0 :(得分:10)
您可以尝试将此功能用作onSelect
事件处理程序:
$('#dob').datepicker({
onSelect: function(value, ui) {
var today = new Date(),
dob = new Date(value),
age = new Date(today - dob).getFullYear() - 1970;
$('#age').text(age);
},
maxDate: '+0d',
yearRange: '1920:2010',
changeMonth: true,
changeYear: true
});
这应该非常准确(并且比我的旧代码好得多),因为所有内容都已传递给本地Date
对象。
在此处查看此简单演示:http://www.jsfiddle.net/yijiang/PHvYK/1
答案 1 :(得分:2)
function getAge(birth) {
var today = new Date();
var curr_date = today.getDate();
var curr_month = today.getMonth() + 1;
var curr_year = today.getFullYear();
var pieces = birth.split('/');
var birth_date = pieces[0];
var birth_month = pieces[1];
var birth_year = pieces[2];
if (curr_month == birth_month && curr_date >= birth_date) return parseInt(curr_year-birth_year);
if (curr_month == birth_month && curr_date < birth_date) return parseInt(curr_year-birth_year-1);
if (curr_month > birth_month) return parseInt(curr_year-birth_year);
if (curr_month < birth_month) return parseInt(curr_year-birth_year-1);
}
var age = getAge('18/01/2011');
alert(age);
答案 2 :(得分:1)
如果你在javascript中减去两个Date
个对象,你会得到它们之间的差异,因此( BDate - (new Date()) )/365.25/24/60/60/1000
会给你一个年龄结果,该结果应该在一天之内准确。 (即如果他们的生日是今天,而闰年则可能是不准确的)
答案 3 :(得分:1)
<script type="text/javascript">
$(function() {
$('#dtp').datepicker({
changeMonth: true,
changeYear: true,
dateFormat: 'mm/dd/yy',
//firstDay: 1,
onSelect: function(dateText, inst) {
var d = new Date(Date.parse(inst.lastVal));
var diff = (new Date()).getFullYear() - d.getFullYear();
document.getElementById('AGE').value = diff;
}
});
});
</script>
答案 4 :(得分:1)
尝试此代码非常简单
function agefinding()
{
var birthDay = document.getElementById("TxtDOB").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));
// alert(age);
return age;
}
使用javascript找到年龄非常简单
答案 5 :(得分:0)
假设您正确构建DOB,请创建:
now = Date() //right now
使用date.getTime()函数以毫秒为单位获取时间。将它们之间的差除以
中的毫秒数one_year = 1000*60*60*24*365;
years = (now.getTime() - DOB.getTime())/one_year;
将其放入回调中,以便在任何日期字段中进行任何更改,并在任意位置显示结果。