【原创】自动生成当月日历表的javascript程序
为了方便每个月做计划,我到网上找了找可供一页纸打印的日历表,但很难找到符合心意的。最后决定自己写一个,日期在左上角,留一大片方块写备注,感觉挺爽的。效果大致如下:
2013年11月日历表 | ||||||
日 | 一 | 二 | 三 | 四 | 五 | 六 |
1 | 2 | |||||
3 | 4 | 5 | 6 | 7 | 8 | 9 |
10 | 11 | 12 | 13 | 14 | 15 | 16 |
17 | 18 | 19 | 20 | 21 | 22 | 23 |
24 | 25 | 26 | 27 | 28 | 29 | 30 |
源代码如下:
保存成html文件用IE打开即可。
<html>
<head>
<script>
function getDaysInOneMonth(year, month){
month = parseInt(month,10)+1;
var d= new Date(year+"/"+month+"/0");
return d.getDate();
}
var now=new Date();
var firstdayofmonth=new Date(now.getFullYear()+'/'+(now.getMonth()+1)+'/1');
function run(id){
var count;
count=1;
temp='<table cellpadding=0 cellspacing=0 border=1 width=1050 height=740 style="text-align:center;vertical-align:top;border-collapse:collapse;"><tr height=5% style="font-size:30px;"><td colspan=7>'+now.getFullYear()+'年'+(now.getMonth()+1)+'月日历表</td></tr><tr height=4% bgcolor=lightgray style="font-size:25px;"><td>日</td><td>一</td><td>二</td><td>三</td><td>四</td><td>五</td><td>六</td></tr>';
temp+='<tr height=13% style="font-size:22px;text-align:left;text-indent:10px;vertical-align:top;line-height:40px;">';
for (var i=0;i<7;i++){
if (id>i) {
temp+='<td></td>';
}else{
temp+='<td>'+count+'</td>';
count++;
}
}
temp+='</tr>';
while (count<=getDaysInOneMonth(now.getFullYear(),(now.getMonth()+1))){
temp+='<tr height=13% style="font-size:22px;text-align:left;text-indent:10px;vertical-align:top;line-height:40px;">';
for (var i=0;i<7;i++){
if(count<=getDaysInOneMonth(now.getFullYear(),(now.getMonth()+1))){
temp+='<td>'+count+'</td>';
count++;
}else{
temp+='<td></td>';
}
}
temp+='</tr>';
}
temp+='</table>';
document.body.innerHTML=temp;
}
</script>
</head>
<body onload="run(firstdayofmonth.getDay())">
</body>
</html>