Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix issue with wrong week number when first day of week is Sunday #301

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/components/Calendar.vue
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
:key="index"
>
<td v-if="showWeekNumbers && (index%7 || index===0)" class="week">
{{ $dateUtil.weekNumber(dateRow[0]) }}
{{ $dateUtil.weekNumber(dateRow[0], locale.firstDay) }}
</td>
<td
v-for="(date, idx) in dateRow"
Expand Down
4 changes: 2 additions & 2 deletions src/components/date_util/native.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ const DateUtil = {
daysInMonth: (year, month) => {
return new Date(year, month, 0).getDate()
},
weekNumber: (date) => {
return getWeek(date)
weekNumber: (date, firstDay) => {
return getWeek(date, firstDay)
},
format: (date, mask) => {
return dateFormat(date, mask)
Expand Down
28 changes: 17 additions & 11 deletions src/components/dateformat.js
Original file line number Diff line number Diff line change
Expand Up @@ -150,26 +150,32 @@ function pad (val, len) {
* @param {Object} `date`
* @return {Number}
*/
function getWeek (date) {
function getWeek (date = new Date(), weekstart) {
// Set default for weekstart and clamp to useful range
if (weekstart === undefined) weekstart = 1; // monday is by default
weekstart %= 7;

// Remove time components of date
var targetThursday = new Date(date.getFullYear(), date.getMonth(), date.getDate());

// Change date to Thursday same week
targetThursday.setDate(targetThursday.getDate() - ((targetThursday.getDay() + 6) % 7) + 3);
// Replaced offset of (6) with (7 - weekstart)
var dayNr = (targetThursday.getDay() + 7 - weekstart) % 7;
targetThursday.setDate(targetThursday.getDate() - dayNr + 3);

// Take January 4th as it is always in week 1 (see ISO 8601)
var firstThursday = new Date(targetThursday.getFullYear(), 0, 4);

// Change date to Thursday same week
firstThursday.setDate(firstThursday.getDate() - ((firstThursday.getDay() + 6) % 7) + 3);
var firstThursday = targetThursday.valueOf();

// Check if daylight-saving-time-switch occurred and correct for it
var ds = targetThursday.getTimezoneOffset() - firstThursday.getTimezoneOffset();
targetThursday.setHours(targetThursday.getHours() - ds);
targetThursday.setMonth(0, 1);
if (targetThursday.getDay() !== 4) {
// Change date to Thursday same week
targetThursday.setMonth(0, 1 + ((4 - targetThursday.getDay()) + 7) % 7);
}

// Number of weeks between target Thursday and first Thursday
var weekDiff = (targetThursday - firstThursday) / (86400000 * 7);
return 1 + Math.floor(weekDiff);

return 1 + Math.ceil((firstThursday - targetThursday) / 604800000);

}

/**
Expand Down