<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<script type="text/javascript">
//作业2:使用函数、switch_case完成给定日期2024 04 20 这一天是本年中的第几天。
//输入年份、月份、天数。
var year = Number(prompt("请输入年份:"));
var month = Number(prompt("请输入月份:"));
var day = Number(prompt("请输入天数:"));
// 判断是否是闰年
function isLeapYear(year){
if((year%4==0&&year%100!=0)||year%400==0){
return true;
}else{
return false;
}
}
function getDaysByDate(year,month,day){
// 定义一个总天数
var days=0;
switch(month){
case 12:
//11月
days+=30;
case 11:
//10月
days+=31;
case 10:
//9月
days+=30;
case 9:
//8月
days+=31;
case 8:
//7月
days+=31;
case 7:
//6月
days+=30;
case 6:
//5月
days+=31;
case 5:
//4月
days+=30;
case 4:
//3月
days+=31;
case 3:
//2月
if(isLeapYear(year)){
days+=29;
}else{
days+=28;
}
case 2:
//1月
days+=31;
case 1:
days+=day;
break;
}
return days;
}
document.write(year+"年"+month+"月"+day+"日是本年中的第:"+getDaysByDate(year,month,day)+"天");
</script>
</head>
<body>
</body>
</html>
使用函数、switch_case完成给定日期2024 04 20 这一天是本年中的第几天。
最新推荐文章于 2024-09-09 20:11:58 发布