Advertisement
Guest User

Untitled

a guest
May 22nd, 2019
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.19 KB | None | 0 0
  1. var Cal = function(divId) {
  2.  
  3. //Store div id
  4. this.divId = divId;
  5.  
  6. // Days of week, starting on Sunday
  7. this.DaysOfWeek = [
  8. 'Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'
  9. ];
  10.  
  11. // Months, stating on January
  12. this.Months = ['January', 'February', 'March', 'April', 'May', 'June', 'July',
  13. 'August', 'September', 'October', 'November', 'December' ];
  14.  
  15. // Set the current month, year
  16. var d = new Date();
  17.  
  18. this.currMonth = d.getMonth();
  19. this.currYear = d.getFullYear();
  20. this.currDay = d.getDate();
  21.  
  22. };
  23.  
  24. // Goes to next month
  25. Cal.prototype.nextMonth = function() {
  26. if ( this.currMonth == 11 ) {
  27. this.currMonth = 0;
  28. this.currYear = this.currYear + 1;
  29. }
  30. else {
  31. this.currMonth = this.currMonth + 1;
  32. }
  33. this.showcurr();
  34. };
  35.  
  36. // Goes to previous month
  37. Cal.prototype.previousMonth = function() {
  38. if ( this.currMonth == 0 ) {
  39. this.currMonth = 11;
  40. this.currYear = this.currYear - 1;
  41. }
  42. else {
  43. this.currMonth = this.currMonth - 1;
  44. }
  45. this.showcurr();
  46. };
  47.  
  48. // Show current month
  49. Cal.prototype.showcurr = function() {
  50. this.showMonth(this.currYear, this.currMonth);
  51. };
  52.  
  53. // Show month (year, month)
  54. Cal.prototype.showMonth = function(y, m) {
  55.  
  56. var d = new Date()
  57. // First day of the week in the selected month
  58. , firstDayOfMonth = new Date(y, m, 1).getDay()
  59. // Last day of the selected month
  60. , lastDateOfMonth = new Date(y, m+1, 0).getDate()
  61. // Last day of the previous month
  62. , lastDayOfLastMonth = m == 0 ? new Date(y-1, 11, 0).getDate() : new Date(y, m, 0).getDate();
  63.  
  64.  
  65. var html = '<table>';
  66.  
  67. // Write selected month and year
  68. html += '<th><tr>';
  69. html += '<td colspan="7">' + this.Months[m] + ' ' + y + '</td>';
  70. html += '</tr></th>';
  71.  
  72.  
  73. // Write the header of the days of the week
  74. html += '<tr class="days">';
  75. for(var i=0; i < this.DaysOfWeek.length;i++) {
  76. html += '<td>' + this.DaysOfWeek[i] + '</td>';
  77. }
  78. html += '</tr>';
  79.  
  80. // Write the days
  81. var i=1;
  82. do {
  83.  
  84. var dow = new Date(y, m, i).getDay();
  85.  
  86. // If Sunday, start new row
  87. if ( dow == 0 ) {
  88. html += '<tr>';
  89. }
  90. // If not Sunday but first day of the month
  91. // it will write the last days from the previous month
  92. else if ( i == 1 ) {
  93. html += '<tr>';
  94. var k = lastDayOfLastMonth - firstDayOfMonth+1;
  95. for(var j=0; j < firstDayOfMonth; j++) {
  96. html += '<td class="not-current">' + k + '</td>';
  97. k++;
  98. }
  99. }
  100.  
  101. // Write the current day in the loop
  102. var chk = new Date();
  103. var chkY = chk.getFullYear();
  104. var chkM = chk.getMonth();
  105. if (chkY == this.currYear && chkM == this.currMonth && i == this.currDay) {
  106. html += '<td class="today">' + i + '</td>';
  107. } else {
  108. html += '<td class="normal">' + i + '</td>';
  109. }
  110. // If Saturday, closes the row
  111. if ( dow == 6 ) {
  112. html += '</tr>';
  113. }
  114. // If not Saturday, but last day of the selected month
  115. // it will write the next few days from the next month
  116. else if ( i == lastDateOfMonth ) {
  117. var k=1;
  118. for(dow; dow < 6; dow++) {
  119. html += '<td class="not-current">' + k + '</td>';
  120. k++;
  121. }
  122. }
  123.  
  124. i++;
  125. }while(i <= lastDateOfMonth);
  126.  
  127. // Closes table
  128. html += '</table>';
  129.  
  130. // Write HTML to the div
  131. document.getElementById(this.divId).innerHTML = html;
  132. };
  133.  
  134. // On Load of the window
  135. window.onload = function() {
  136.  
  137. // Start calendar
  138. var c = new Cal("divCal");
  139. c.showcurr();
  140.  
  141. // Bind next and previous button clicks
  142. getId('btnNext').onclick = function() {
  143. c.nextMonth();
  144. };
  145. getId('btnPrev').onclick = function() {
  146. c.previousMonth();
  147. };
  148. }
  149.  
  150. // Get element by id
  151. function getId(id) {
  152. return document.getElementById(id);
  153. }
  154.  
  155.  
  156.  
  157. ======================================================HTML==================================================
  158.  
  159. <div class="cal">
  160. <div class="calendar-wrapper">
  161. <input type="image" src="{% static '/images/previousButton.png' %}" id="btnPrev" width="25px">
  162. <input type="image" src="{% static '/images/nextButton.png' %}" id="btnNext" width="25px">
  163. <div id="divCal"></div>
  164. </div>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement