【发布时间】:2017-05-05 07:37:24
【问题描述】:
我是 JS 的初学者。我可以用下面的代码计算年龄。这个代码非常适合计算年数。如果我把日期放在 12 之后 (例如. 14/12/1950) 任何一个月,然后显示 NaN 年。现在我想显示 年、月、日 的年龄从任何人的生日。有人可以帮我吗?提前致谢。
$(document).ready(function ()
{
console.log($(document).width());
$('#datepicker').datepicker
({
dateFormat: 'dd/mm/yy',
changeMonth: true,
changeYear: true,
yearRange: '1900:2150',
maxDate: new Date(),
inline: true,
onSelect: function() {
var birthDay = document.getElementById("datepicker").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('agecal').innerText = age;
}
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
<link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
<div class="form-group">
<label for="datepicker">Date of Birth: <span style="color:red">*</span></label>
<input type="text" id="datepicker" name="date_of_birth" class="form-control" placeholder="Insert your Date of Birth...">
</div>
<div class="form-group">
<label for="agecal">Age as On(<?php echo date('d/m/Y'); ?>): <span id="agecal" style="background-color:#60ab59;padding: 0px 50px 0px 50px;color: white;font-weight: bold; border-radius: 5px;" >0</span></label>
</div>
【问题讨论】:
标签: javascript jquery laravel jquery-ui