从出生日期计算年龄
我有两个文本框。第一个是约会,第二个是约会。我有两种情况:从出生日期计算年龄
- 我知道这个生日,我填写文本框和年龄进行计算,并在第二个文本框显示,这个文本框是只读的
- 我不知道生日,我进入一个年龄,没有生日,没有更多的动作
你有第一点的想法吗?
克里斯,
我也有类似的问题,前一段时间使用此页面作为参考:
http://www.javascriptkit.com/javatutors/datedifference.shtml
或本:
http://snippets.dzone.com/posts/show/623
只是包装你的调用所需的jquery按钮点击等。
个欢呼
--markup--
<input id = "eAge" type = "text"/>
<input type= "button" id = "fAge" value = "find"/>
<input id = "age" type = "text"/>
--jQuery --
$("#fAge").click(function() {
var currentYear = (new Date).getFullYear();
var year = $("#eAge").val().split(',');
$("#age").val(currentYear - year[1]);
});
这里的的jsfiddle链接到它 - http://jsfiddle.net/GGgqx/
希望它帮助
的输入格式,我收到“NaN”作为结果。 – 2011-05-23 18:50:25
你给了什么输入?并且我给出的解决方案是基于格式为[[month date],[year]'的输入),您必须根据您的要求对其进行自定义:) – 2011-05-23 19:22:21
-1因为您的解决方案不会计算已录入的价值已在历年发生 – 2012-01-25 17:09:00
我的功能:
$('#txtFecha').focusout(function() {
var fecha = document.getElementById('txtFecha').value;
var edad = '';
//supose my date is dd/mm/yyyy
if ((fecha.substring(2,3) != "/") && (fecha.substring(5,6) != "/")) {
edad = '';
}
else {
var dia = fecha.substring(0, 2);
var mes = fecha.substring(3, 5);
var anio = fecha.substring(6, 10);
var diaActual = (new Date).getDay();
var mesActual = (new Date).getMonth();
var anioActual = (new Date).getFullYear();
if (mes > mesActual)
edad = anioActual - anio - 1;
if (mesActual == mes && dia > diaActual)
edad = anioActual - anio - 1;
edad = anioActual - anio;
if (edad < 0 || edad > 100)
edad = '';
}
$("#txt2Edad").val(edad);
});
什么日期格式那会进入?如果输入日期有一个标准格式,为什么你不能分割字符串,获得出生年份并找出差异? – 2011-05-23 11:58:01
@Tarun:说我出生于2000年7月1日,现在是2011年5月23日。我几岁了? – 2011-05-23 11:58:51
是的,所以我问的是格式标准?是[月份日期],[年]' – 2011-05-23 12:00:45