Advertisement
Guest User

Monthly Calendar

a guest
Oct 11th, 2018
235
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function calendar([day, month, year]) {
  2.  
  3.     let input = [+day, +month, +year];
  4.  
  5.     let days = [];
  6.  
  7.     let prevMonthLastDay = new Date(input[2], input[1] - 1, 0);
  8.  
  9.     if (month - 1 < 0) {
  10.         prevMonthLastDay = new Date(input[2] - 1, 12, 31);
  11.     }
  12.  
  13.     if (prevMonthLastDay.getDay() !== 6) {
  14.         for (let i = prevMonthLastDay.getDay(); i >= 0; i--) {
  15.             days.push(prevMonthLastDay.getDate() - i);
  16.         }
  17.     }
  18.  
  19.     let currentMonthDays = new Date(input[2], input[1], 0).getDate();
  20.  
  21.     for (let i = 1; i <= currentMonthDays; i++) {
  22.         if (i === input[0]) {
  23.             days.push(100 + i);
  24.         } else {
  25.             days.push(i);
  26.         }
  27.     }
  28.  
  29.     let nextMonthFirstDay = new Date(input[2], input[1], 1);
  30.     let a = nextMonthFirstDay.getDay();
  31.     if (nextMonthFirstDay.getDay() !== 0) {
  32.         for (let i = nextMonthFirstDay.getDate(); i <= 6; i++) {
  33.             days.push(i);
  34.         }
  35.     }
  36.  
  37.     let result = "<table>";
  38.     result += "\r\n";
  39.     result += "<tr><th>Sun</th><th>Mon</th><th>Tue</th>" +
  40.         "<th>Wed</th><th>Thu</th><th>Fri</th><th>Sat</th></tr>";
  41.     result += "\r\n";
  42.  
  43.     let isPrevMonth = true;
  44.     let isNextMonth = true;
  45.  
  46.     let weekCount = days.length / 7;
  47.  
  48.     for (let week = 1; week <= weekCount; week++) {
  49.         result += "  <tr>";
  50.         for (let day = 1; day <= 7; day++) {
  51.  
  52.             let currentDay = days[0];
  53.             days.shift();
  54.  
  55.             if (currentDay === 1 || currentDay === 101) {
  56.                 isPrevMonth = false;
  57.                 isNextMonth = !isNextMonth;
  58.             }
  59.  
  60.             if (isPrevMonth) {
  61.                 result += `<td class="prev-month">${currentDay}</td>`;
  62.             } else if (isNextMonth) {
  63.                 result += `<td class="next-month">${currentDay}</td>`;
  64.             } else if (currentDay > 100) {
  65.                 currentDay -= 100;
  66.                 result += `<td class="today">${currentDay}</td>`;
  67.             } else {
  68.                 result += `<td>${currentDay}</td>`;
  69.             }
  70.         }
  71.         result += "</tr>";
  72.         result += "\r\n";
  73.     }
  74.  
  75.     result += "</table>";
  76.  
  77.     console.log(result);
  78. }
  79.  
  80. // calendar([1, 4, 2016]);
  81. calendar([24, 12, 2012]);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement