我有2个HTML字段,分别称为出生日期和年龄。
有一个按钮来计算年龄。现在我想在不按下按钮的情况下计算这个年龄。它应自动显示我在出生日期字段中输入完成后的年龄。如何用JavaScript做到这一点?
<tr>
<td>Birth Date</td>
<td><input type="text" name="birth_date" id="datepicke" value="" maxlength="11"
placeholder="birth date" /></td>
</tr>
<tr>
<td>Age</td>
<td>
<button type="button" onclick="displayDate()">Display Difference</button>
<p id="demo" style="vertical-align: top">
<input type="text" name="age" value="" maxlength="3" placeholder="age" /></td>
</tr>
<script>
function displayDate()
{
var dof = document.getElementById('datepicke').value;
var d1 = new Date(dof); //from date yyyy-MM-dd
var d2 = new Date(); //to date yyyy-MM-dd (taken currentdate)
var Months = d2.getMonth() - d1.getMonth();
var Years = d2.getFullYear() - d1.getFullYear();
var Days = d2.getDate() - d1.getDate();
Months = (d2.getMonth() + 12 * d2.getFullYear()) -
(d1.getMonth() + 12 * d1.getFullYear());
var MonthOverflow = 0;
if (Months - (Years * 12) < 0)
MonthOverFlow = -1;
else
MonthOverFlow = 1;
if (MonthOverFlow < 0)
Years = Years - 1; Months = Months - (Years * 12);
var LastDayOfMonth = new Date(d2.getFullYear(),
d2.getMonth() + 1, 0, 23, 59, 59);
LastDayOfMonth = LastDayOfMonth.getDate();
if (MonthOverFlow < 0 && (d1.getDate() > d2.getDate())) {
Days = LastDayOfMonth + (d2.getDate() - d1.getDate()) - 1;
}
else
Days = d2.getDate() - d1.getDate();
if (Days < 0)
Months = Months - 1;
var l = new Date(d2.getFullYear(), d2.getMonth(), 0);
var l1 = new Date(d1.getFullYear(), d1.getMonth() + 1, 0);
if (Days < 0)
{
if (l1 > l)
Days = l1.getDate() + Days;
else
Days = l.getDate() + Days;
}
document.getElementById("demo").innerHTML = Years +
" Year(s)";
//, " + Months + " Month(s), " + Days + "Day(s
}
</script>
更新
<tr>
<td>Birth Date</td>
<td><input onblur="displayDate()" type="text" name="birth_date" id="datepicke" value=""
maxlength="11" placeholder="birth date" /></td>
</tr>
<tr>
<td>Age</td>
<td><input type="text" name="age" id="age" value="" maxlength="3" placeholder="age" /></td>
</tr>
Javascript代码:
<script>
function displayDate()
{
var dof = document.getElementById('datepicke').value;
var d1 = new Date(dof); //from date yyyy-MM-dd
var d2 = new Date(); //to date yyyy-MM-dd (taken currentdate)
var Months = d2.getMonth() - d1.getMonth();
var Years = d2.getFullYear() - d1.getFullYear();
var Days = d2.getDate() - d1.getDate();
Months = (d2.getMonth() + 12 * d2.getFullYear()) -
(d1.getMonth() + 12 * d1.getFullYear());
var MonthOverflow = 0;
if (Months - (Years * 12) < 0)
MonthOverFlow = -1;
else
MonthOverFlow = 1;
if (MonthOverFlow < 0)
Years = Years - 1; Months = Months - (Years * 12);
var LastDayOfMonth = new Date(d2.getFullYear(),
d2.getMonth() + 1, 0, 23, 59, 59);
LastDayOfMonth = LastDayOfMonth.getDate();
if (MonthOverFlow < 0 && (d1.getDate() > d2.getDate())) {
Days = LastDayOfMonth + (d2.getDate() - d1.getDate()) - 1;
}
else
Days = d2.getDate() - d1.getDate();
if (Days < 0)
Months = Months - 1;
var l = new Date(d2.getFullYear(), d2.getMonth(), 0);
var l1 = new Date(d1.getFullYear(), d1.getMonth() + 1, 0);
if (Days < 0)
{
if (l1 > l)
Days = l1.getDate() + Days;
else
Days = l.getDate() + Days;
}
document.getElementById("age").innerHTML = Years +
" Year(s)";
//, " + Months + " Month(s), " + Days + "Day(s
}
</script>
答案 0 :(得分:1)
用作
<input onchange="displayDate()" type="text" name="birth_date" id="datepicke" value="" maxlength="11" placeholder="birth date" />
答案 1 :(得分:0)
您可以在输入中添加onblur
事件。当您单击该字段时,这将触发。如果您使用onchange
,则可能会因日期错误而导致错误。
<input onblur="displayDate()" type="text" name="birth_date" id="datepicke" value="" maxlength="11" placeholder="birth date" />