我从https://gist.github.com/vincentorback/8edffeca20e7a9e5e2a6那里获得了这段代码,并且想知道一些有关它的信息。
/**
* Gets the ISO week number for a given date.
* @param d either Date or number,the date to get week number for.
* @returns The ISO week number for given date.
*/
function getWeekNumber(d: any) {
// copy given date
d = new Date(+d);
d.setHours(0,0);
// Set date to nearest thursday
// If getDay is 0 it's sunday,we change the day number to 7 making it the last day of the week
d.setDate(d.getDate() + 4 - (d.getDay() || 7));
const yearStart: any = new Date(d.getFullYear(),1);
// First calculate delta of start of year and given date in milliseconds: (d - yearStart)
// Then we divide that by 86.400.000 ((d - yearStart) / 1000 / 60 / 60 / 24 ) to know difference in days
// Then we calculate the number of weeks
return Math.ceil((((d - yearStart) / 86400000) + 1) / 7); // <-- why + 1?
}
我真的不明白为什么在这里做+ 1
。还有,为什么星期天将天数更改为7?
我试图评论我确实发现的事情,但是如果仍然不清楚,请告诉我!