本节内容:
javascript日历
例子:
<script>
/**
* javascript 日历
* by www.jb200.com
*/
function Calender()
{
var str="日,一,二,三,四,五,六";
weekArray=str.split(',');
var now=new Date();
this.year=now.getFullYear();
this.day=now.getDay();
this.month=now.getMonth()+1;
this.date=now.getDate();
//定义输出函数
this.print=function()
{
//输出第一行
var lineStr="<div style='width:200px' align='center'>"+
this.year+"年"+this.month+"月"
;
if(this.isRunYear(this.year))
lineStr+="(闰)</div>";
lineStr+="<div align='center'>星期"+weekArray[this.day];
lineStr+="</div>";
//输出今天的日期
dateStr="<div align='center'><h1>"+this.date+"</h1></div>";
//日历星期和日期的对应
var weeks="<div align='center'><table><tr>";
for (var i = 0; i < weekArray.length; i++)
{
weeks += "<td>" + weekArray[i] + "</td>";
}
weeks+="</tr>";
var firstday=new Date(this.year+"/"+this.month+"/1");
var fweek=firstday.getDay();
theday=now.getDate();
daynum=this.haveDays(this.year,this.month);
for(var j=1;j<7;j++)
{
weeks+="<tr>";
for(var i=0;i<7;i++)
{
theday=7*(j-1)+i+1-fweek;
if ((j==0)&&i<(fweek)||(theday>daynum)||theday<=0)
weeks+="<td> </td>";
else
weeks+="<td>"+theday+"</td>";
}
weeks+="</tr>";
}
weeks+="</table></div>";
document.writeln(lineStr);
document.writeln(dateStr);
document.writeln(weeks);
}
//得到年该月的第一天是星期几
this.getFirstDay=function()
{
var firstday=new Date(this.year+"/"+this.month+"/1");
return firstday.getDate();
}
//检查是否是闰年
this.isRunYear=function(year)
{
if((year%400==0)||((year%4==0)&&(year%100!=0)))
return true;
else
return false;
}
//判断这某年某月有几天
this.haveDays=function(year,month)
{
var result=0;
switch(month)
{
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:result=31;break;
case 4:
case 6:
case 9:
case 11:result=30;break;
case 2:result=(isRunYear(year))?29:28;break;
}
return result;
}
}
window.onload=function()
{
var calender=new Calender();
calender.print();
}