Guest User

Untitled

a guest
Sep 28th, 2018
249
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.39 KB | None | 0 0
  1. function calendar([day, month, year]) {
  2. day = Number(day);
  3. month = Number(month);
  4. year = Number(year);
  5.  
  6. let date = new Date(year, month - 1, day);
  7. let nextMonthDays = [];
  8. let prevMonthDays = [];
  9. let currentMonthDays = [];
  10.  
  11. // Current month
  12. let currentMonth = new Date(date);
  13. currentMonth.setDate(1);
  14. let temp = new Date(currentMonth);
  15. temp.setMonth(currentMonth.getMonth() + 1);
  16. temp.setDate(0);
  17. let stop = temp.getDate();
  18. while (true) {
  19. currentMonthDays.push(new Date(currentMonth));
  20. if ((currentMonth.getDate() == stop))
  21. break;
  22. currentMonth.setDate(currentMonth.getDate() + 1);
  23. }
  24.  
  25. // Next month
  26. let nextMonth = new Date(date);
  27. nextMonth.setMonth(nextMonth.getMonth() + 1);
  28. nextMonth.setDate(1);
  29. while (true) {
  30. if (nextMonth.getDay() == 0)
  31. break;
  32. nextMonthDays.push(new Date(nextMonth));
  33. nextMonth.setDate(nextMonth.getDate() + 1);
  34. };
  35.  
  36. // Previous month
  37. let prevMonth = new Date(date);
  38. prevMonth.setDate(0);
  39. while (true) {
  40. prevMonthDays.push(new Date(prevMonth));
  41. if (prevMonth.getDay() == 0)
  42. break;
  43. prevMonth.setDate(prevMonth.getDate() - 1);
  44. };
  45. prevMonthDays.reverse();
  46.  
  47. // open table
  48. let html = "<table>\n";
  49.  
  50. // Append headers
  51. html +=
  52. "<tr><th>Sun</th><th>Mon</th>" +
  53. "<th>Tue</th><th>Wed</th><th>Thu</th>" +
  54. "<th>Fri</th><th>Sat</th></tr>\n";
  55. // Flow helpers
  56. let counter = 0;
  57. let flow = -1;
  58. let currentMonthLen = currentMonthDays.length;
  59. let prevMonthLen = prevMonthDays.length;
  60. let nextMonthLen = nextMonthDays.length;
  61. let boundary = currentMonthDays[0].getDay() == 0
  62. ? (currentMonthLen + nextMonthLen) / 7
  63. : (currentMonthLen + prevMonthLen + nextMonthLen) / 7;
  64.  
  65. // Append cells to table
  66. for (let row = 0; row < boundary; row++) {
  67. html += " <tr>";
  68.  
  69. for (let col = 0; col < 7; col++) {
  70. // Append previous month days
  71. if (counter < prevMonthLen && flow == -1) {
  72. // fix the flow if current month's starting day is Sat
  73. if (currentMonthDays[0].getDay() == 0) {
  74. flow++;
  75. counter = 0;
  76. col--;
  77. continue;
  78. }
  79. html += `<td class="prev-month">${prevMonthDays[counter].getDate()}</td>`;
  80. }
  81. // Control the flow
  82. if (counter == prevMonthLen && flow == -1 ||
  83. counter == currentMonthLen && flow == 0) {
  84. flow++;
  85. counter = 0;
  86. }
  87. // Append current month days
  88. if (counter < currentMonthLen && flow == 0) {
  89. if (currentMonthDays[counter].getDate() == day)
  90. html += `<td class="today">${day}</td>`;
  91. else
  92. html += `<td>${currentMonthDays[counter].getDate()}</td>`;
  93. }
  94. // Append next month days
  95. if (counter < nextMonthLen && flow == 1) {
  96. html += `<td class="next-month">${nextMonthDays[counter].getDate()}</td>`;
  97. }
  98. counter++;
  99. }
  100.  
  101. html += "</tr>\n";
  102. }
  103.  
  104. // close table
  105. html += "</table>";
  106. return html;
  107. }
Advertisement
Add Comment
Please, Sign In to add comment