代码如下:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
input {
100px;
}
#btn {
150px;
}
</style>
</head>
<body>
<input type="text" id="txt1">
<input type="text" id="txtm">
<input type="text" id="txt2">
<input type="button" id="btn" value="这一天是今年的多少天?">
<input type="text" id="txt3">
</body>
</html>
<script>
var otxt1 = document.getElementById("txt1");
var osel = document.getElementById("txtm");
var otxt2 = document.getElementById("txt2");
var obtn = document.getElementById("btn");
var otxt3 = document.getElementById("txt3");
obtn.onclick = function () {
var n = Number(otxt1.value);
var y = Number(osel.value);
var r = Number(otxt2.value);
var aaa = justify(n, y, r);
if (aaa) {
otxt3.value = Date(n, y, r);
} else {
$("#txt3").value("");
}
}
function justify(n, y, r) {
if (n < 1980 || n > 2020) {
alert("请输入1980-2020年的年份");
return false;
}
if (y < 1 || y > 12) {
alert("请输入1-12之间的月份");
return false;
}
if (n % 4 == 0 && n % 100 != 0 || n % 400 == 0) {
if (y == 2) {
if (r > 29) {
alert("该年是闰年,2月总共29天");
return false;
}
}
} else {
if (y == 2) {
if (r > 28) {
alert("该年是平年,2月总共28天");
return false;
}
}
}
switch (y) {
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
if (r < 0 || r > 31) {
alert("超出日期范围");
return false;
}
break;
case 4:
case 6:
case 9:
case 11:
if (r < 0 || r > 30) {
alert("超出日期范围");
return false;
}
break;
case 2:
if (r < 0 || r > 29) {
alert("超出日期范围");
return false;
}
}
return true;
}
function Date(year, month, day) {
var sum = 0;
switch (month) {
case 12:
sum += 30;
case 11:
sum += 31;
case 10:
sum += 30;
case 9:
sum += 31;
case 8:
sum += 31;
case 7:
sum += 30;
case 6:
sum += 31;
case 5:
sum += 30;
case 4:
sum += 31;
case 3:
if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) {
sum += 29;
} else {
sum += 28;
}
case 2:
sum += 31;
case 1:
sum += day;
break;
default:
sum = "请输入正确的月份"
}
return sum;
}
</script>