使用函数完成给定日期,这一天是本年的第几天

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>

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值