<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<form>
<input type="text" name="date1" id="date1" />到<input type="text" name="date2" id="date2" />
<input type="button" id="button" value="计算" />
</form>
<p id="p1"></p>
<script src="js/jquery-3.2.1.min.js"></script>
<script>
function getDaysIndex(date) {
var result = date.split("-");
var year = parseInt(result[0]);
var month = parseInt(result[1]);
var day = parseInt(result[2]);
var leapYears = getLeapYears(year - 1);
var monthDays = [ 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 ];
return (year - 1) * 365 + leapYears + monthDays[month - 1] + day + (month > 2 && isLeapYear(year) ? 1 : 0);
}
function isLeapYear(year) {
return year % 400 == 0 || (year % 4 == 0 && year % 100 != 0);
}
function getLeapYears(year) {
return Math.floor(year / 4) - Math.floor(year / 100) + Math.floor(year / 400);
}
$(function() {
$('#button').click(function() {
var result = getDaysIndex($("#date2").val()) - getDaysIndex($("#date1").val());
$("#p1").html("共" + result + "天");
});
});
</script>
</body>
</html>
使用jquery实现计算两个日期差
最新推荐文章于 2022-08-01 15:48:50 发布