Advertisement
Guest User

Untitled

a guest
Mar 23rd, 2018
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.08 KB | None | 0 0
  1. window.onload = function() {
  2.  
  3. function renderCalendar() {
  4. const now = moment();
  5. const [day, month, year] = [now.date(), now.format('MM'), now.year()];
  6. const endOfMonth = now.endOf('month').format('DD');
  7. const prevMonthLength = now.subtract(1, 'months').endOf('month').format('DD');
  8.  
  9. let [dayOfWeek, week] = [now.toDate().getDay() - 2, 0];
  10.  
  11. let html = "<table>\n <tr><th>Sun</th><th>Mon</th><th>Tue</th><th>Wed</th><th>Thu</th><th>Fri</th><th>Sat</th></tr>\n";
  12. // other function
  13.  
  14. [html, week] = prevDaysOfPrevMonth(html, dayOfWeek, prevMonthLength, week); // Render Previous days of prev mount
  15. [html, week] = daysOfCurrMonth(html, endOfMonth, week, day, month, year); // Render current month days
  16. [html, week] = daysOfNextMonth(html, week); // Render next mount days
  17.  
  18. document.getElementById('currentDate').innerHTML = moment().format('DD, MMMM YYYY');
  19. document.getElementById('main').innerHTML = html;
  20.  
  21. Array.from(document.getElementsByClassName('daysOfCurrMonth')).forEach((td) => {
  22. td.addEventListener('click', showWeekday);
  23. });
  24. }
  25.  
  26. renderCalendar();
  27.  
  28. function showWeekday(year,month,day) {
  29.  
  30. console.log(moment(`${year}-${month}-${day}`).format('dddd'));
  31. //console.log(event.target.hasAttribute('data-year'));
  32. //event.target.setAttribute("data-id", "anotherId");
  33. //weekday
  34. // if(event.target.hasAttribute('data-year')){
  35. //
  36. // }
  37. //let weekDay = moment(`${event.target.dataset.year}-${event.target.dataset.month}-${event.target.dataset.day}`).format('dddd');
  38.  
  39. }
  40.  
  41. function prevDaysOfPrevMonth(outerHtml, daysOfWeek, prevMonthL, weekLen) {
  42.  
  43. if (daysOfWeek > 0) {
  44. outerHtml += ' <tr>';
  45. }
  46. for (let i = Number(prevMonthL - daysOfWeek); i <= Number(prevMonthL); i++) {
  47. outerHtml += '<td class="prevMonth daysOfCurrMonth">' + i + '</td>';
  48. weekLen++;
  49. }
  50. return [outerHtml, weekLen];
  51. }
  52.  
  53. function daysOfCurrMonth(outerHtml, end, weekLen, day, month, year) {
  54.  
  55. for (let j = 1; j <= end; j++) {
  56. if (weekLen == 0) {
  57. outerHtml += " <tr>";
  58. }
  59. let today = '';
  60. if (day == j) {
  61. today = 'today';
  62. //outerHtml += `<td class="daysOfCurrMonth today" >` + j + '</td>';
  63. }
  64. outerHtml += `<td class="daysOfCurrMonth ${today}" data-month="${month}" data-year="${year}" data-day="${j}">` + j + '</td>';
  65.  
  66. weekLen++;
  67. if (weekLen == 7) {
  68. outerHtml += '</tr> \n';
  69. weekLen = 0;
  70. }
  71. }
  72. return [outerHtml, weekLen];
  73. }
  74.  
  75. function daysOfNextMonth(outerHtml, weekLen) {
  76.  
  77. for (let k = 1; weekLen != 0; k++) {
  78. outerHtml += '<td class="nextMonth daysOfCurrMonth">' + k + '</td>';
  79. weekLen++;
  80. if (weekLen == 7) {
  81. outerHtml += '</tr> \n';
  82. weekLen = 0;
  83. }
  84. }
  85. return [outerHtml, weekLen];
  86. }
  87.  
  88.  
  89.  
  90. }
  91.  
  92.  
  93. /*
  94. Leap year Algorithm
  95. let daysInMonth = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
  96. if (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0)){
  97. daysInMonth[1] = 29;
  98. }
  99. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement