2016-11-13 10 views
1

私はカレンダーを作成しようとしていますが、1月30日か31日かを月に応じて印刷したいのですが、その合計日数は変数totalDaysですネストされたループにtrtd年代を作成したが、日を埋めるために30セル内のすべての日付を1日から合計日まで印刷する方法

var date = new Date('11/13/2016'); 

     var totalDays = new Date(date.getFullYear(), date.getMonth() + 1, 0); 

     var daysArr = ['M', 'T', 'W', 'T', 'F', 'S', 'S']; 

     totalDays = totalDays.getDate();   

     var table = document.createElement('table'); 


     for (var i = 0; i <= 5+1; i++) { 

      var tr = table.insertRow(i); 

      for(var ii = 0; ii <= 7-1; ii++){ 
       var td = tr.insertCell(ii); 
       td.innerHTML = (ii); 


      } 
     } 

     console.log(table); 
+0

2つのループの長さは同じです。ちょうど1つのループを削除 – prasanth

答えて

0

に1から各td内の値を印刷する方法がわからない、あなたは試すことができ、この1:

var date = new Date('11/13/2016'), 
 
    totalDays = new Date(date.getFullYear(), date.getMonth() + 1, 0), 
 
    // i changed the order to synchronize with getDay() in js => Sun = 0 
 
    daysArr = ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'], 
 
    table = document.createElement('table'), 
 
    row = 0, 
 
    d = 1, 
 
    h = new Date(date.setDate(1)); 
 

 
totalDays = totalDays.getDate(); 
 
h = h.getDay(); 
 

 
while (d <= totalDays + daysArr.length) { 
 
    var tr = table.insertRow(row); 
 
    for (var ii = 0; ii < daysArr.length; ii++) { 
 
    if (d > totalDays + daysArr.length) { 
 
     break; 
 
    } 
 
    var td = tr.insertCell(ii); 
 
    if (d <= daysArr.length) { 
 
     //filling the Header for calendar 
 
     td.innerHTML = daysArr[h]; 
 
     d++; 
 
     if (h >= daysArr.length - 1) { 
 
     h = 0; 
 
     } else { 
 
     h++ 
 
     } 
 
    } else { 
 
     //filling the dates 
 
     td.innerHTML = d++ - daysArr.length; 
 
    } 
 
    } 
 
    row++; 
 
} 
 

 

 
document.body.appendChild(table); 
 
console.log('Total Days this month:', totalDays);

もちろん、あなたのカレンダープロジェクトはまだ完了していません。この助けを願って

関連する問題