2016-11-13 2 views
1

Ich versuche, einen Kalender zu machen, und ich Datum von 1 bis 30 oder 31 drucken möchte, hängt von dem Monat ab, und ich habe die gesamten Tage in Variable totalDays, ich habe erstellt tr und td ‚s in verschachtelten Schleifen, aber nicht sicher, wie Wert innerhalb jedes td von 1 bis 30.Wie alle Daten in Zellen von 1 bis totalDays

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

zwei für die Länge der Schlaufe sind gleich. nur eine Schleife entfernen – prasanth

Antwort

0

bis zu drucken, um die Tage zu füllen, können Sie versuchen, diese ein:

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);

Natürlich ist es noch nicht fertig für Ihr Kalenderprojekt. aber hoffe, dass diese Hilfe

Verwandte Themen