js写日历

这几天对js进行深入学习:最近打的一个代码:输入一个年份,输入该年的日历
效果如下:
在这里插入图片描述
在这里插入图片描述
js代码如下:

	<script type="text/javascript">
		function calendar(y){
			//获取指定月份1月1日的星期数值
			var w=new Date(y,0).getDay();
			
			var html='<div class="box">';
			//拼接每个月份的表格
			for(m=1;m<=12;++m){
				html+='<table>';
				html+='<tr class="title"><th colspan="7">'+y+'年'+m+'月</th></tr>';
				html+='<tr><td>日</td><td>一</td><td>二</td><td>三</td><td>四</td><td>五</td><td>六</td></tr>';
				
				//获取月份m共有多少天
				var max =new Date(y,m,0).getDate();
				
				html+='<tr>';
				for(d=1;d<=max;++d){
					if(w&&d==1){
						html+='<td colspan="'+w+'"></td>';
						
					}
					html+='<td>'+d+'</td>';
					if(w==6&&d!=max){
						html +='</tr><tr>';
					}else if(d==max){
						html+='</tr>';
					}
					w=(w+1>6)?0:w+1;
				}
				html+='</table>';
			}
			html+='</div>';
			return html;
		}
		
		
		var year=parseInt(prompt('请输入年份:','2018'));
		document.write(calendar(year));
	</script>
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值