Javascript 给定出生日期时自动获取年龄
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4151480/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Getting age automatically when given Date of Birth
提问by yopirates
I wanted to get the age of a person from his date of birth. I have a HTML code where I'm using a datepicker for dob, when I give the date-of-birth it show automatically show the age without giving submit. Please help me in finding a solution for this.
我想从他的出生日期得到一个人的年龄。我有一个 HTML 代码,我在其中使用了 dob 的日期选择器,当我给出出生日期时,它会自动显示年龄而不提交。请帮助我找到解决方案。
<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>
回答by Yi Jiang
You can try using this function as your onSelect
event handler instead:
您可以尝试使用此函数作为您的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
});
This should be very accurate (and much better than my old code), since everything's been handed off to the native Date
object.
这应该非常准确(并且比我的旧代码好得多),因为一切都已移交给本机Date
对象。
See a simple demonstration of this here: http://www.jsfiddle.net/yijiang/PHvYK/1
在这里看到一个简单的演示:http: //www.jsfiddle.net/yijiang/PHvYK/1
回答by paulinho
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);
回答by Sakthi Karthik
Try This Code very simple
试试这个代码非常简单
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;
}
this is very simple to find the age using javascript
使用javascript查找年龄非常简单
回答by TheVillageIdiot
<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>
回答by tobyodavies
If you subtract two Date
objects in javascript, you get their difference in milliseconds so ( BDate - (new Date()) )/365.25/24/60/60/1000
will give you an age result that should be accurate to within a day. (i.e. if their birthday is today and its a leap year it may be inaccurate)
如果你Date
在 javascript 中减去两个对象,你会得到它们的差异以毫秒为单位,所以( BDate - (new Date()) )/365.25/24/60/60/1000
会给你一个应该精确到一天之内的年龄结果。(即,如果他们的生日是今天并且是闰年,则可能不准确)
回答by jon_darkstar
Assuming you construct DOB properly, create:
假设您正确构建了 DOB,请创建:
now = Date() //right now
use the date.getTime() function to get time in milliseconds. Divide the difference of them by by the milliseconds in a
使用 date.getTime() 函数以毫秒为单位获取时间。将它们的差异除以毫秒数
one_year = 1000*60*60*24*365;
years = (now.getTime() - DOB.getTime())/one_year;
Put this in a callback for any change in any of your date fields and display the result wherever you like.
将它放在任何日期字段中的任何更改的回调中,并在您喜欢的任何地方显示结果。