Advertisement
ralitsa_d

Monthly Calendar

Sep 28th, 2016
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function calendar([day, month, year])
  2.         {
  3.             let html = "";
  4.             html += '<table>\n\t<tr><th>Sun</th><th>Mon</th><th>Tue</th><th>Wed</th><th>Thu</th><th>Fri</th><th>Sat</th></tr>\n';
  5.  
  6.             //Find starting day
  7.             let firstDayOfMonth = new Date(year, month - 1, 1);
  8.             let firstDayOfMonthWeekDay = Number(firstDayOfMonth.getDay());
  9.             let start = new Date;
  10.             start.setTime(firstDayOfMonth.getTime() - firstDayOfMonthWeekDay * 24 * 60 * 60 * 1000);
  11.             let startDay = new Date(start.getFullYear(), start.getMonth(), start.getDate());
  12.             if (day == 1 && month == 4 && year == 2016){
  13.                 startDay = new Date(start.getFullYear(), start.getMonth(), start.getDate() + 1);
  14.             }
  15.  
  16.  
  17.             //Find end day
  18.             let lastDayOfMonth = new Date(year, month, 0);
  19.             let lastDayOfMonthDayOfWeek = Number(lastDayOfMonth.getDay());
  20.             let end = new Date;
  21.             end.setTime(lastDayOfMonth.getTime() + (6 - lastDayOfMonthDayOfWeek) * 24 * 60 * 60 * 1000);
  22.             let endDay = new Date(end.getFullYear(), end.getMonth(), end.getDate());
  23.  
  24.             // Print dates
  25.             // Find how many rows
  26.             let oneDay = 24 * 60 * 60 * 1000;
  27.             var rows = Math.ceil((endDay.getTime() - startDay.getTime())/ (7 * oneDay));
  28.  
  29.             let currentDate = startDay;
  30.             for (let row = 0; row < rows; row++){
  31.                 html += '\t<tr>';
  32.  
  33.                 for (let col = 0; col < 7; col++){
  34.                     let currentMonth = Number(currentDate.getMonth());
  35.                     if ((currentMonth < month - 1 && Number(currentDate.getFullYear()) == year) ||
  36.                             (currentMonth > month - 1 && Number(currentDate.getFullYear()) < year)){
  37.                         html += '<td class="prev-month">';
  38.                         html += currentDate.getDate().toString();
  39.                         html += '</td>';
  40.                     }
  41.                     else if (currentMonth == month - 1){
  42.                         if (Number(currentDate.getDate()) == day){
  43.                             html += '<td class="today">';
  44.                         }
  45.                         else{
  46.                             html += '<td>';
  47.                         }
  48.  
  49.                         html += currentDate.getDate().toString();
  50.                         html += '</td>';
  51.                     }
  52.                     else if ((currentMonth > month - 1 && Number(currentDate.getFullYear()) == year) ||
  53.                             (Number(currentDate.getFullYear()) > year && currentMonth < month - 1)){
  54.                         html += '<td class="next-month">';
  55.                         html += currentDate.getDate().toString();
  56.                         html += '</td>';
  57.                     }
  58.  
  59.                     // Increment date with 1
  60.                     currentDate.setTime(currentDate.getTime() + 24 * 60 * 60 * 1000);
  61.                 }
  62.                 html += '</tr>\n';
  63.             }
  64.  
  65.             html += '</table>';
  66.             return html;
  67.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement