gantt-task-react
gantt-task-react copied to clipboard
Week numbers not reporting correct numbers
Hello @MaTeMaTuK
After running the npm run test
and failing on the getWeekNumberISO8601 function
I added code to compare the output of the function with 2 other methods of calculation of week number. Using the other methods both returned the same week number which returned the expected value in the test
export const getWeekNumberISO8601 = (date: Date) => {
const tmpDate = new Date(date.valueOf());
const dayNumber = (tmpDate.getDay() + 6) % 7;
tmpDate.setDate(tmpDate.getDate() - dayNumber + 3);
const firstThursday = tmpDate.valueOf();
tmpDate.setMonth(0, 1);
if (tmpDate.getDay() !== 4) {
tmpDate.setMonth(0, 1 + ((4 - tmpDate.getDay() + 7) % 7));
}
const weekNumber = (
1 + Math.ceil((firstThursday - tmpDate.valueOf()) / 604800000)
).toString();
// https://weeknumber.com/how-to/javascript
var date2 = new Date(date.valueOf());
date2.setHours(0, 0, 0, 0);
// Thursday in current week decides the year.
date2.setDate(date2.getDate() + 3 - (date2.getDay() + 6) % 7);
// January 4 is always in week 1.
var week1 = new Date(date2.getFullYear(), 0, 4);
// Adjust to Thursday in week 1 and count number of weeks from date to week1.
const weekNumber2 = (1 + Math.round(((date2.getTime() - week1.getTime()) / 86400000
- 3 + (week1.getDay() + 6) % 7) / 7)).toString();
// https://www.epoch-calendar.com/support/getting_iso_week.html
var date3 = new Date(date.valueOf());
const dowOffset = 1; //default to day starting monday
var newYear = new Date(date3.getFullYear(),0,1);
var day = newYear.getDay() - dowOffset; //the day of week the year begins on
day = (day >= 0 ? day : day + 7);
var daynum = Math.floor((date3.getTime() - newYear.getTime() -
(date3.getTimezoneOffset()-newYear.getTimezoneOffset())*60000)/86400000) + 1;
var weekNumber3;
//if the year starts before the middle of a week
if(day < 4) {
weekNumber3 = Math.floor((daynum+day-1)/7) + 1;
if(weekNumber3 > 52) {
const nYear = new Date(date3.getFullYear() + 1,0,1);
let nday = nYear.getDay() - dowOffset;
nday = nday >= 0 ? nday : nday + 7;
/*if the next year starts before the middle of
the week, it is week #1 of that year*/
weekNumber3 = nday < 4 ? 1 : 53;
}
}
else {
weekNumber3 = Math.floor((daynum+day-1)/7);
}
console.log(weekNumber,weekNumber2,weekNumber3.toString());
if (weekNumber.length === 1) {
return `0${weekNumber}`;
} else {
return weekNumber;
}
};
Result from example app console.log after changing to Week format
23 22 22
22 21 21
21 20 20
20 19 19
19 18 18
18 17 17
17 16 16
16 15 15
15 14 14
13 13 13
Hi @Graeme43, Thanks for your work. I have seen your request. Unfoturanlty, During the war I cannnot work as before. But I will take a look this week. Thanks :)
Hi, I have checked my method here and it works correct.
Hi @MaTeMaTuK,
I guess that leaves the question as why the tests fail with the 20 Jun 2020 test for me