两种方法 分别是if 和 switch
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<script>
// 拿到年 判断是否闰年
// 拿到月 获取比它小的月份
// 拿到天 用来累加
var year = parseInt(prompt('请输入年份'));
var month = parseInt(prompt('请输入月份'));
var day = parseInt(prompt('请输入天'));
if (month > 1) {
day += 31;
}
if (month > 2) {
day += ((year % 4 == 0 && year % 100 != 0) || (year % 100 == 0)) ? 29 : 28;
}
if (month > 3) {
day += 31;
}
if (month > 4) {
day += 30;
}
if (month > 5) {
day += 31;
}
if (month > 6) {
day += 30;
}
if (month > 7) {
day += 31;
}
if (month > 8) {
day += 31;
}
if (month > 9) {
day += 30;
}
if (month > 10) {
day += 31;
}
if (month > 11) {
day += 30;
}
alert('今天是' + year + '年的第' + day + '天');
</script>
<script>
// switch方法:
var year = parseInt(prompt('请输入年份'));
var month = parseInt(prompt('请输入月份'));
var day = parseInt(prompt('请输入天'));
switch (month) {
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
console.log('31天');
break
case 2:
console.log('28天');
break
case 4:
case 6:
case 9:
case 11:
console.log('30天');
break
default:
console.log('请输入1-12的数字即可');
}
alert('今天是' + year + '年的第' + day + '天');
</script>
</body>
</html>