js生成当月カレンダー

2900 ワード

var date = new Date();
//      
var year = date.getFullYear();
//      ,        0     
var month = date.getMonth();

//          
var firstDay = new Date(year,month,1);
//       
var weekday = firstDay.getDay();

//          
//new Date(year,month+1,0) : month+1     ,day 0             ,              。
//getDate()                  ,                  
var days = new Date(year,month+1,0).getDate();

var res = "";
//          
for(var i=0;i<weekday;i++){
    res+="  ";
}

for(var j=1;j<=days ;j++){
    res += j+" ";
    weekday++;

    //        
    if(weekday%7 == 0){
           weekday = 0;
           res += '
'
; } }