Advertisement
Guest User

Untitled

a guest
Sep 30th, 2016
185
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function calendar([day, month, year]){
  2.     let html = '<table>\n';
  3.     let currDay = Number(day);
  4.     let currMonth = Number(month) - 1;
  5.     let currYear = Number(year);
  6.     let currDate = new Date(currYear, currMonth, currDay);
  7.     let firstOfMonth = new Date(currYear, currMonth);          
  8.     let lastOfMonth = new Date(currYear, currMonth+1, 0);
  9.     let calendarStartDay = new Date(currYear, currMonth, 1-firstOfMonth.getDay());
  10.     let calendarLastDay = new Date(currYear, currMonth+1, 6-lastOfMonth.getDay());
  11.    
  12.     //Prints table header
  13.     html+=" <tr><th>Sun</th><th>Mon</th><th>Tue</th><th>Wed</th><th>Thu</th><th>Fri</th><th>Sat</th></tr>\n";
  14.    
  15.     //Prints prev month days
  16.     if(firstOfMonth.getDay()>0) {
  17.     html += ' <tr>';
  18.     }
  19.     for (let i = calendarStartDay.getDay(); i<firstOfMonth.getDay(); i++){             
  20.         html+='<td class="prev-month">'+(calendarStartDay.getDate()+i)+'</td>';
  21.     }
  22.    
  23.     //Prints curr mont days, bolds current day
  24.     let tempDay = 1;               
  25.     while(tempDay <= lastOfMonth.getDate()){
  26.         //first day of the week opens new table row
  27.         if(new Date(currYear, currMonth, tempDay).getDay() == 0){
  28.             html +=" <tr>";
  29.         }
  30.         if(tempDay == currDay){
  31.             html+='<td class="today">'+tempDay+'</td>';
  32.         }
  33.         else {
  34.             html+='<td>'+tempDay+'</td>';
  35.         }
  36.        
  37.         //last day of the week closes table row
  38.         if((new Date(currYear, currMonth, tempDay).getDay()+1) % 7 == 0){
  39.             html +='</tr>\n';
  40.         }              
  41.         tempDay++;
  42.     }
  43.    
  44.     //Prints next month days
  45.     if(lastOfMonth<calendarLastDay){
  46.         for (let i = 1; i <= calendarLastDay.getDate(); i++){
  47.             html+='<td class="next-month">'+i+'</td>';
  48.         }                                      
  49.         html += '</tr>\n'
  50.     }
  51.    
  52.     html += '</table>'
  53.     return html;
  54. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement