1:问题描述:使用函数完成给定日期,这一天是本年的第几天
2:解题思路
2.1:数据的获取
var year=prompt("请输入年份")
year=parseInt(year)
var month=prompt("请输入月份")
month=parseInt(month)
var day=prompt("请输入日期")
day=parseInt(day)
2.2:判断年份
2.3:根据平润年计算月份天数
// 2:判断年份
function isLeapYear(year){
if((year%4==0&&year%100!=0)||year%400==0){
// 3:根据平润年计算月份天数
switch(month){
case 1:
totalDays=day
break
case 2:
totalDays=31+day
break
case 3:
totalDays=31+28+day
break
case 4:
totalDays=31+28+31+day
break
case 5:
totalDays=31+28+31+30+day
break
case 6:
totalDays=31+28+31+30+31+day
break
case 7:
totalDays=31+28+31+30+31+30+day
break
case 8:
totalDays=31+28+31+30+31+30+31+day
break
case 9:
totalDays=31+28+31+30+31+30+31+31+day
break
case 10:
totalDays=31+28+31+30+31+30+31+31+30+day
break
case 11:
totalDays=31+28+31+30+31+30+31+31+30+31+day
break
case 12:
totalDays=31+28+31+30+31+30+31+31+30+31+30+day
break
}
document.write(year+"年"+month+"月"+day+"日"+"是今年的第"+totalDays+"天")
}else{
switch(month){
case 1:
totalDays=day
break
case 2:
totalDays=31+day
break
case 3:
totalDays=31+29+day
break
case 4:
totalDays=31+29+31+day
break
case 5:
totalDays=31+29+31+30+day
break
case 6:
totalDays=31+29+31+30+31+day
break
case 7:
totalDays=31+29+31+30+31+30+day
break
case 8:
totalDays=31+29+31+30+31+30+31+day
break
case 9:
totalDays=31+29+31+30+31+30+31+31+day
break
case 10:
totalDays=31+29+31+30+31+30+31+31+30+day
break
case 11:
totalDays=31+29+31+30+31+30+31+31+30+31+day
break
case 12:
totalDays=31+29+31+30+31+30+31+31+30+31+30+day
break
}
document.write(year+"年"+month+"月"+day+"日"+"是今年的第"+totalDays+"天")
}
}
2.4:判断天数总和(定义一个变量接受天数总和)
var totalDays=0
2.5:输出
isLeapYear(year)
3:重难点总结,首先要判断年份是平年还是闰年 然后根据年份给二月赋值,通过switc循环来计算天数总和最后输出
4:完整源码
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>使用函数完成给定日期,这一天是本年的第几天</title>
<script>
// 2:判断年份
function isLeapYear(year){
if((year%4==0&&year%100!=0)||year%400==0){
// 3:根据平润年计算月份天数
switch(month){
case 1:
totalDays=day
break
case 2:
totalDays=31+day
break
case 3:
totalDays=31+28+day
break
case 4:
totalDays=31+28+31+day
break
case 5:
totalDays=31+28+31+30+day
break
case 6:
totalDays=31+28+31+30+31+day
break
case 7:
totalDays=31+28+31+30+31+30+day
break
case 8:
totalDays=31+28+31+30+31+30+31+day
break
case 9:
totalDays=31+28+31+30+31+30+31+31+day
break
case 10:
totalDays=31+28+31+30+31+30+31+31+30+day
break
case 11:
totalDays=31+28+31+30+31+30+31+31+30+31+day
break
case 12:
totalDays=31+28+31+30+31+30+31+31+30+31+30+day
break
}
document.write(year+"年"+month+"月"+day+"日"+"是今年的第"+totalDays+"天")
}else{
switch(month){
case 1:
totalDays=day
break
case 2:
totalDays=31+day
break
case 3:
totalDays=31+29+day
break
case 4:
totalDays=31+29+31+day
break
case 5:
totalDays=31+29+31+30+day
break
case 6:
totalDays=31+29+31+30+31+day
break
case 7:
totalDays=31+29+31+30+31+30+day
break
case 8:
totalDays=31+29+31+30+31+30+31+day
break
case 9:
totalDays=31+29+31+30+31+30+31+31+day
break
case 10:
totalDays=31+29+31+30+31+30+31+31+30+day
break
case 11:
totalDays=31+29+31+30+31+30+31+31+30+31+day
break
case 12:
totalDays=31+29+31+30+31+30+31+31+30+31+30+day
break
}
document.write(year+"年"+month+"月"+day+"日"+"是今年的第"+totalDays+"天")
}
}
// 1:数据的获取
var year=prompt("请输入年份")
year=parseInt(year)
var month=prompt("请输入月份")
month=parseInt(month)
var day=prompt("请输入日期")
day=parseInt(day)
// 4:判断天数总和(定义一个变量接受天数总和)
var totalDays=0
// 5:输出第几天
isLeapYear(year)
</script>
</head>
<body>
</body>
</html>