|
139 | 139 |
|
140 | 140 | // `getWeekOfYear` returns the week number for the year |
141 | 141 | Date.prototype.getWeekOfYear = function () { |
142 | | - var ys = new Date(this.getFullYear(), 0, 1); |
143 | | - var sd = new Date(this.getFullYear(), this.getMonth(), this.getDate()); |
144 | | - if (ys.getDay() > 3) { |
145 | | - ys = new Date(sd.getFullYear(), 0, (7 - ys.getDay())); |
146 | | - } |
147 | | - var daysCount = sd.getDayOfYear() - ys.getDayOfYear(); |
148 | | - return Math.ceil(daysCount / 7); |
149 | | - |
| 142 | + var d = new Date(this.valueOf()); |
| 143 | + d.setHours(0, 0, 0); |
| 144 | + // Set to nearest Thursday: current date + 4 - current day number |
| 145 | + // Make Sunday's day number 7 |
| 146 | + d.setDate(d.getDate() + 4 - (d.getDay() || 7)); |
| 147 | + // Get first day of year |
| 148 | + var yearStart = new Date(d.getFullYear(), 0, 1); |
| 149 | + // Calculate full weeks to nearest Thursday |
| 150 | + var weekNo = Math.ceil((((d - yearStart) / 86400000) + 1) / 7); |
| 151 | + // Return week number |
| 152 | + return weekNo; |
150 | 153 | }; |
151 | 154 |
|
152 | 155 | // `getDaysInMonth` returns the number of days in a month |
153 | 156 | Date.prototype.getDaysInMonth = function () { |
154 | 157 | return 32 - new Date(this.getFullYear(), this.getMonth(), 32).getDate(); |
155 | 158 | }; |
| 159 | + |
| 160 | + // `getWeekYear` return the year from the actual week number, not from the date (e.g. December 31th, 2014 it's actually in week 1 from 2015) |
| 161 | + Date.prototype.getWeekYear = function () { |
| 162 | + var d = new Date(this.valueOf()); |
| 163 | + d.setHours(0, 0, 0); |
| 164 | + // Set to nearest Thursday: current date + 4 - current day number |
| 165 | + // Make Sunday's day number 7 |
| 166 | + d.setDate(d.getDate() + 4 - (d.getDay() || 7)); |
| 167 | + |
| 168 | + return d.getFullYear(); |
| 169 | + }; |
| 170 | + |
| 171 | + // `getWeekMonth` return the month from the actual week number, not from the date (e.g. December 31th, 2014 it's actually in January, 2015) |
| 172 | + Date.prototype.getWeekMonth = function () { |
| 173 | + var d = new Date(this.valueOf()); |
| 174 | + d.setHours(0, 0, 0); |
| 175 | + // Set to nearest Thursday: current date + 4 - current day number |
| 176 | + // Make Sunday's day number 7 |
| 177 | + d.setDate(d.getDate() + 4 - (d.getDay() || 7)); |
| 178 | + |
| 179 | + return d.getMonth(); |
| 180 | + }; |
156 | 181 |
|
157 | 182 | // `hasWeek` returns `true` if the date resides on a week boundary |
158 | 183 | // **????????????????? Don't know if this is true** |
|
567 | 592 | var rday = range[i]; |
568 | 593 |
|
569 | 594 | // Fill years |
570 | | - if (rday.getFullYear() !== year) { |
| 595 | + if (rday.getWeekYear() !== year) { |
571 | 596 | yearArr.push( |
572 | 597 | ('<div class="row header year" style="width: ' |
573 | 598 | + tools.getCellSize() * daysInYear |
574 | 599 | + 'px;"><div class="fn-label">' |
575 | 600 | + year |
576 | 601 | + '</div></div>')); |
577 | | - year = rday.getFullYear(); |
| 602 | + year = rday.getWeekYear(); |
578 | 603 | daysInYear = 0; |
579 | 604 | } |
580 | 605 | daysInYear++; |
581 | 606 |
|
582 | 607 | // Fill months |
583 | | - if (rday.getMonth() !== month) { |
| 608 | + if (rday.getWeekMonth() !== month) { |
584 | 609 | monthArr.push( |
585 | 610 | ('<div class="row header month" style="width:' |
586 | 611 | + tools.getCellSize() * daysInMonth |
587 | 612 | + 'px;"><div class="fn-label">' |
588 | 613 | + settings.months[month] |
589 | 614 | + '</div></div>')); |
590 | | - month = rday.getMonth(); |
| 615 | + month = rday.getWeekMonth(); |
591 | 616 | daysInMonth = 0; |
592 | 617 | } |
593 | 618 | daysInMonth++; |
|
737 | 762 | + '</div></div>'); |
738 | 763 |
|
739 | 764 | var dataPanel = core.dataPanel(element, range.length * tools.getCellSize()); |
740 | | - |
741 | | - |
742 | | - // Append panel elements |
743 | | - |
744 | | - dataPanel.append(yearArr.join("")); |
745 | | - dataPanel.append(monthArr.join("")); |
746 | | - dataPanel.append($('<div class="row" style="margin-left: 0;" />').html(dayArr.join(""))); |
747 | | - dataPanel.append($('<div class="row" style="margin-left: 0;" />').html(dowArr.join(""))); |
| 765 | + |
| 766 | + dataPanel.append(yearArr.join("") + monthArr.join("") + dayArr.join("") + (dowArr.join(""))); |
748 | 767 |
|
749 | 768 | break; |
750 | 769 | } |
|
0 commit comments