Advertisement
Guest User

Untitled

a guest
Sep 25th, 2016
412
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function genCalendar([day, month, year]) {
  2.     [day, month, year] = [day, month, year].map(Number)
  3.  
  4.     let today = day
  5.     let firstDayInCurrMonth = new Date(year, month - 1, 1).getDay()
  6.     let lastDayInCurrMonth = new Date(year, month, 0).getDay()
  7.  
  8.     let prevMonthLength = new Date(year, month - 1, 0).getDate()
  9.     let currMonthLenght = new Date(year, month, 0).getDate()
  10.  
  11.     let prevMonth = getPrevMonth(firstDayInCurrMonth, prevMonthLength)
  12.     let currMonth = getCurrMonth(currMonthLenght)
  13.     let nextMonth = getNextMonth(6 - lastDayInCurrMonth)
  14.  
  15.     currMonth[today - 1] = `<td class="today">${today}</td>`
  16.  
  17.     let allFormatedDates = [].concat(prevMonth, currMonth, nextMonth)
  18.     let calendarRows = allFormatedDates.length / 7
  19.  
  20.     let html = '<table>\n'
  21.     html += '\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'
  22.     let row = ''
  23.  
  24.     for (let week = 0; week < calendarRows; week++) {
  25.         row = '\t<tr>'
  26.         for (let index = week * 7; index < week * 7 + 7; index++) {
  27.             row += allFormatedDates[index]
  28.         }
  29.         row += '</tr>\n'
  30.         html += row
  31.     }
  32.  
  33.     html += '</table>'
  34.  
  35.     return html
  36.  
  37.     function getPrevMonth(datesCount, currDate) {
  38.         currDate -= datesCount - 1
  39.         return new Array(datesCount).fill(0).map(() => {return `<td class="prev-month">${currDate++}</td>`})
  40.     }
  41.    
  42.     function getCurrMonth(monthLength) {
  43.         let currDate = 1
  44.         return new Array(monthLength).fill(0).map(() => {return `<td>${currDate++}</td>`})
  45.     }
  46.  
  47.     function getNextMonth(datesCount) {
  48.         let currDate = 1
  49.         return new Array(datesCount).fill(0).map(() => {return `<td class="next-month">${currDate++}</td>`})
  50.     }
  51. }
  52.  
  53. console.log(genCalendar([24, 12, 2012]))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement