Guest User

Simple Calendar

a guest
Oct 25th, 2016
225
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const $ = jQuery;
  2. const monthNames = [
  3.     "January", "February", "March", "April", "May", "June",
  4.     "July", "August", "September", "October", "November", "December"
  5. ];
  6.  
  7. const dayNames = [
  8.     "Mon",
  9.     "Tue",
  10.     "Wed",
  11.     "Thu",
  12.     "Fri",
  13.     "Sat",
  14.     "Sun"
  15. ];
  16.  
  17. function calendar(dateProps) {
  18.     if (dateProps && dateProps.length === 3) {
  19.         let dateInput = new Date(`${dateProps[1]}.${dateProps[0]}.${dateProps[2]}`),
  20.             totalMonthDays = new Date(dateProps[2], dateProps[1], 0).getDate(),
  21.             dayNumber = dateInput.getDay(),
  22.             daysInWeek = [],
  23.             weeksInMonth = [],
  24.             calendarMarkup = '',
  25.             today = new Date().getDate();
  26.  
  27.         for (let notADay = 0; notADay < dayNumber; notADay += 1) {
  28.             daysInWeek.push(0);
  29.         }
  30.  
  31.         for (let currentDay = 1; currentDay <= totalMonthDays; currentDay += 1) {
  32.             daysInWeek.push(currentDay);
  33.  
  34.             // 7, 14, 21, 28
  35.             if ((dayNumber + currentDay) % 7 == 0) {
  36.                 weeksInMonth.push(daysInWeek);
  37.                 daysInWeek = [];
  38.                
  39.                 // only on 28
  40.                 if (currentDay + 7 > totalMonthDays && currentDay != totalMonthDays) {
  41.                     let leftDays = [];
  42.  
  43.                     // from 29 to the end of the month
  44.                     for (let leftDay = currentDay + 1; leftDay <= totalMonthDays; leftDay += 1) {
  45.                         leftDays.push(leftDay);
  46.                     }
  47.  
  48.                     weeksInMonth.push(leftDays);
  49.                 }
  50.             }
  51.         }
  52.  
  53.         let lastWeekInMonth = weeksInMonth[weeksInMonth.length - 1];
  54.         if (lastWeekInMonth.length < 7) {
  55.             while(lastWeekInMonth.length < 7) {
  56.                 lastWeekInMonth.push(0);
  57.             }
  58.         }
  59.  
  60.         let daysHeader = '';
  61.         dayNames.forEach(dayName => {
  62.             daysHeader += `<th>${dayName}</th>`;
  63.         });
  64.        
  65.         let daysBody = '';
  66.         weeksInMonth.forEach(week => {
  67.             daysBody += '<tr>';
  68.             week.map(dayInWeek => {
  69.                 daysBody += `<td ${dayInWeek == today ? 'class="today"':''}>${dayInWeek || ''}</td>`;
  70.             })
  71.             daysBody += '</tr>';
  72.         });
  73.  
  74.         calendarMarkup +=
  75.         `
  76.             <table>
  77.                 <caption>${monthNames[dateInput.getMonth()]} ${dateInput.getFullYear()}</caption>
  78.                 <tbody>
  79.                     <tr>
  80.                         ${daysHeader}
  81.                     </tr>
  82.                     ${daysBody}
  83.                 </tbody>
  84.             </table>
  85.         `;
  86.  
  87.         $('#content').append(calendarMarkup);
  88.     }
  89. }
Add Comment
Please, Sign In to add comment