Guest User

Untitled

a guest
Jan 31st, 2018
207
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.49 KB | None | 0 0
  1. function calendar (calendarDate) {
  2. let [day, month, year] = calendarDate
  3. let date = new Date(year, month - 1, 0)
  4. let daysInMonth = new Date(year, month, 0).getDate()
  5.  
  6. let monthNames = [
  7. 'January', 'February', 'March', 'April', 'May', 'June', 'July',
  8. 'August', 'September', 'October', 'November', 'December']
  9.  
  10. let root = $('#content')
  11. let tableBody = $('<tbody>')
  12.  
  13. tableBody.append($('<tr>')
  14. .append($('<th>').text('Mon'))
  15. .append($('<th>').text('Tue'))
  16. .append($('<th>').text('Wed'))
  17. .append($('<th>').text('Thu'))
  18. .append($('<th>').text('Fri'))
  19. .append($('<th>').text('Sat'))
  20. .append($('<th>').text('Sun')))
  21.  
  22. date.setDate(date.getDate() - date.getDay())
  23.  
  24. for (let row = 0; row < 4; row++) {
  25. let tableRow = $('<tr>')
  26. tableBody.append(tableRow)
  27.  
  28. for (let col = 0; col < 7; col++) {
  29. date.setDate(date.getDate() + 1)
  30.  
  31. if (date.getMonth() === month - 1) {
  32. if (date.getDate() === day) {
  33. tableRow.append($('<td>').addClass('today').text(`${date.getDate()}`))
  34. } else {
  35. tableRow.append($('<td>').text(`${date.getDate()}`))
  36. }
  37. } else {
  38. tableRow.append($('<td>'))
  39. }
  40. }
  41.  
  42. if (date.getMonth() === month - 1 &&
  43. row === 3 &&
  44. daysInMonth > date.getDate()) {
  45. row--
  46. }
  47. }
  48.  
  49. let table = $('<table>')
  50. .append($('<caption>').text(`${monthNames[month - 1]} ${year}`))
  51. .append(tableBody)
  52.  
  53. root.append(table)
  54. }
Advertisement
Add Comment
Please, Sign In to add comment