var date = new Date();


function createCalendar(whichDiv)
{
  var strCalendar = "";
  
  // 每月日數陣列
  var monthDays = new Array(31,28,31,30,31,30,31,31,30,31,30,31);
  
  // 閏年判斷
  mYear = date.getFullYear();
  if ( ( (mYear % 4 == 0) && (mYear % 100 != 0) ) || (mYear % 400 == 0) ) monthDays[1] = 29;
  
  // 設定日期為該月第一天
  date.setDate(1);
  
  // 該月第一天的星期
  var day = date.getDay();
  
  // 計算秀出時需要的格數
  var total = monthDays[date.getMonth()] + day;
  var totalCells = total + ( total%7 ? 7 - total%7 : 0  ); 


  strCalendar ='<TABLE cellSpacing="1" cellPadding="0" align="center" width="400" bgcolor="#888888">';
  strCalendar += '<TR>';
  strCalendar += '<TH></TH>';
  strCalendar += "<TH>日</TH>";
  strCalendar += '<TH>一</TH>';
  strCalendar += '<TH>二</TH>';
  strCalendar += '<TH>三</TH>';
  strCalendar += '<TH>四</TH>';
  strCalendar += '<TH>五</TH>';
  strCalendar += '<TH>六</TH>';
  strCalendar += '</TR>';


  for (i=0;i<totalCells;i++)
  {
if ( i%7 == 0 )
strCalendar+="<TR><TD>&nbsp;</TD>";


if ( i >= day && i < total )
{
if ( i >= day )
{
var whichDate = date.getFullYear() + "/" + (date.getMonth()+1) + "/" + ((i-day)+1);
strCalendar += "<TD>"+((i-day)+1);
strCalendar += "<input id='Identity' type='hidden' value='" + whichDate + "'>";
}
}
else
{
strCalendar += "<TD>&nbsp;";
}


strCalendar += "</TD>";


if ( i%7 == 6 )
strCalendar += "</TR>";
   }

   document.getElementById(whichDiv).innerHTML = strCalendar;
}




呼叫的話就用,像以下就會產生2011年1月的日曆
date.setFullYear('2011');
date.setMonth(0);
createCalendar('myDiv');

welkingunther 發表在 痞客邦 PIXNET 留言(0) 人氣()