Advertisement
Guest User

Monthly Calendar

a guest
Sep 27th, 2018
222
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 (prevMonthLastDay.getDay() !== 6) {
  10.         for (let i = prevMonthLastDay.getDay(); i >= 0; i--) {
  11.             days.push(prevMonthLastDay.getDate() - i);
  12.         }
  13.     }
  14.  
  15.     let currentMonthDays = new Date(input[2], input[1], 0).getDate();
  16.  
  17.     for (let i = 1; i <= currentMonthDays; i++) {
  18.         if (i === input[0]) {
  19.             days.push(100 + i);
  20.         } else {
  21.             days.push(i);
  22.         }
  23.     }
  24.  
  25.     let nextMonthFirstDay = new Date(input[2], input[1], 1);
  26.  
  27.     if (nextMonthFirstDay.getDay() !== 0) {
  28.         for (let i = 1; i <= nextMonthFirstDay.getDate(); i++) {
  29.             days.push(i);
  30.         }
  31.     }
  32.  
  33.     let result = "<table>";
  34.     result += "\r\n";
  35.     result += "  <tr><th>Sun</th><th>Mon</th><th>Tue</th>" +
  36.         "<th>Wed</th><th>Thu</th><th>Fri</th><th>Sat</th></tr>";
  37.     result += "\r\n";
  38.  
  39.     let isPrevMonth = true;
  40.     let isNextMonth = true;
  41.  
  42.     let weekCount = days.length / 7;
  43.  
  44.     for (let week = 1; week <= weekCount; week++) {
  45.         result += "  <tr>";
  46.         for (let day = 1; day <= 7; day++) {
  47.  
  48.             let currentDay = days[0];
  49.             days.shift();
  50.  
  51.             if (currentDay === 1 || currentDay === 101) {
  52.                 isPrevMonth = false;
  53.                 isNextMonth = !isNextMonth;
  54.             }
  55.  
  56.             if (isPrevMonth) {
  57.                 result += `<td class="prev-month">${currentDay}</td>`;
  58.             } else if (isNextMonth) {
  59.                 result += `<td class="next-month">${currentDay}</td>`;
  60.             } else if (currentDay > 100) {
  61.                 currentDay -= 100;
  62.                 result += `<td class="today">${currentDay}</td>`;
  63.             } else {
  64.                 result += `<td>${currentDay}</td>`;
  65.             }
  66.         }
  67.         result += "</tr>";
  68.         result += "\r\n";
  69.     }
  70.  
  71.     result += "</table>";
  72.  
  73.    return result;
  74. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement