页面中三个输入框,输入年月日,一个按钮,点击得到此日期在当年中是第几天(考虑闰年和平年)
代码:
<!DOCTYPE html>
<html>
<head>
<!-- 1、页面中三个输入框,输入年月日,一个按钮,点击得到此日期在当年中是第几天(考虑闰年和平年) -->
<meta charset="utf-8">
<title></title>
</head>
<body>
<input type="text" id="year" placeholder="请输入年:" />
<input type="text" id="month" placeholder="请输入月:" />
<input type="text" id="day" placeholder="请输入日:" />
<input type="button" id="btn" value="提交" />
</body>
<script type="text/javascript">
var yearInput = document.getElementById("year");
var monthInput = document.getElementById("month");
var dayInput = document.getElementById("day");
var btn = document.getElementById("btn");
btn.onclick = function() {
var year = Number(yearInput.value);
var month = Number(monthInput.value);
var day = Number(dayInput.value);//强制转换类型
var months = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
var days = 0;
for (var i = 0; i < month - 1; i++) {
days = days + months[i];
}
if (month >= 2) {
if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) {
days = days + 1;
}
}
days = days + day;
alert(days);
}
</script>
</html>
showtime: