From 8f0068227e848b29e3c53da5f7ab185c40f05a3a Mon Sep 17 00:00:00 2001 From: martakostova Date: Fri, 18 Mar 2022 09:24:44 -0400 Subject: [PATCH 1/2] update getWeek because it returned wrong week N --- src/components/dateformat.js | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/src/components/dateformat.js b/src/components/dateformat.js index 6669cde3..e6fefc66 100644 --- a/src/components/dateformat.js +++ b/src/components/dateformat.js @@ -150,7 +150,8 @@ function pad (val, len) { * @param {Object} `date` * @return {Number} */ -function getWeek (date) { +function getWeek (currentDate = new Date()) { + /* // Remove time components of date var targetThursday = new Date(date.getFullYear(), date.getMonth(), date.getDate()); @@ -170,6 +171,24 @@ function getWeek (date) { // Number of weeks between target Thursday and first Thursday var weekDiff = (targetThursday - firstThursday) / (86400000 * 7); return 1 + Math.floor(weekDiff); + */ + + if (!currentDate || !(currentDate instanceof Date)) { + return 0; + } + + const date = new Date(currentDate.getTime()); + date.setHours(0, 0, 0, 0); + + date.setDate(date.getDate() + 3 - ((date.getDay() + 6) % 7)); + + const firstWeek = new Date(date.getFullYear(), 0, 4); + const MILLISECONDS_IN_DAY = 86400000; // 24 * 60 * 60 * 1000 + + const currentYearDay = (date.getTime() - firstWeek.getTime()) / MILLISECONDS_IN_DAY; + + return 1 + Math.round((currentYearDay - 3 + ((firstWeek.getDay() + 6) % 7)) / 7); + } /** From 6be331b09fc4e9992c6cd746be5c4d4ed8b53749 Mon Sep 17 00:00:00 2001 From: martakostova Date: Fri, 18 Mar 2022 22:00:02 +0500 Subject: [PATCH 2/2] update getWeek to support different firstDay of week --- src/components/Calendar.vue | 2 +- src/components/date_util/native.js | 4 +-- src/components/dateformat.js | 43 +++++++++++------------------- 3 files changed, 18 insertions(+), 31 deletions(-) diff --git a/src/components/Calendar.vue b/src/components/Calendar.vue index 49b9f1bb..cba93261 100644 --- a/src/components/Calendar.vue +++ b/src/components/Calendar.vue @@ -29,7 +29,7 @@ :key="index" > - {{ $dateUtil.weekNumber(dateRow[0]) }} + {{ $dateUtil.weekNumber(dateRow[0], locale.firstDay) }} { 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) diff --git a/src/components/dateformat.js b/src/components/dateformat.js index e6fefc66..7068298a 100644 --- a/src/components/dateformat.js +++ b/src/components/dateformat.js @@ -150,44 +150,31 @@ function pad (val, len) { * @param {Object} `date` * @return {Number} */ -function getWeek (currentDate = new 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); - - // Number of weeks between target Thursday and first Thursday - var weekDiff = (targetThursday - firstThursday) / (86400000 * 7); - return 1 + Math.floor(weekDiff); - */ - - if (!currentDate || !(currentDate instanceof Date)) { - return 0; + targetThursday.setMonth(0, 1); + if (targetThursday.getDay() !== 4) { + // Change date to Thursday same week + targetThursday.setMonth(0, 1 + ((4 - targetThursday.getDay()) + 7) % 7); } - const date = new Date(currentDate.getTime()); - date.setHours(0, 0, 0, 0); - - date.setDate(date.getDate() + 3 - ((date.getDay() + 6) % 7)); - - const firstWeek = new Date(date.getFullYear(), 0, 4); - const MILLISECONDS_IN_DAY = 86400000; // 24 * 60 * 60 * 1000 - - const currentYearDay = (date.getTime() - firstWeek.getTime()) / MILLISECONDS_IN_DAY; + // Number of weeks between target Thursday and first Thursday - return 1 + Math.round((currentYearDay - 3 + ((firstWeek.getDay() + 6) % 7)) / 7); + return 1 + Math.ceil((firstThursday - targetThursday) / 604800000); }