2 回答
TA贡献1811条经验 获得超4个赞
我们需要一个函数来计算周的总计数:
function getISOWeeks(y) {
var d,
isLeap;
d = new Date(y, 0, 1);
isLeap = new Date(y, 1, 29).getMonth() === 1;
//check for a Jan 1 that's a Thursday or a leap year that has a
//Wednesday jan 1. Otherwise it's 52
return d.getDay() === 4 || isLeap && d.getDay() === 3 ? 53 : 52
}
还有一个得到周数的函数:
function getWeek(date_string) {
let [d, M, y] = date_string.split(/[- :]/);
let passedDate = new Date(y, parseInt(M) - 1, d);
let onejan = new Date(passedDate.getFullYear(), 0, 1);
week = Math.ceil( (((passedDate - onejan) / 86400000) + onejan.getDay() + 1) / 7 );
return week;
}
在我们经历了这周之后,我们每周计算人数。然后我们可以计算出一整年中每周有多少人在工作。
let weeks = dateRanges.map(s => s.map(d => this.getWeek(d)));
let distinctWeeks = weeks.map(s =>
s.filter((item, pos) => s.indexOf(item) == pos)).flatMap(s => s);
let personPerWeek = distinctWeeks.reduce((a, c)=> {
a[c] = a[c] || 0;
a[c] += 1;
return a;
},{})
const weekCount = getISOWeeks(2020);
let personsPerWeeks = [];
getWorkWeeks = (workWeeks, weekCount) => {
for (let index = 1; index <= weekCount; index++) {
let personCount = personPerWeek[index] || 0;
workWeeks.push(personCount);
}
return workWeeks;
}
一个例子可以在这里看到:
function getWeek(date_string) {
let [d, M, y] = date_string.split(/[- :]/);
let passedDate = new Date(y, parseInt(M) - 1, d);
let onejan = new Date(passedDate.getFullYear(), 0, 1);
week = Math.ceil( (((passedDate - onejan) / 86400000) + onejan.getDay() + 1) / 7 );
return week;
}
function getISOWeeks(y) {
var d,
isLeap;
d = new Date(y, 0, 1);
isLeap = new Date(y, 1, 29).getMonth() === 1;
//check for a Jan 1 that's a Thursday or a leap year that has a
//Wednesday jan 1. Otherwise it's 52
return d.getDay() === 4 || isLeap && d.getDay() === 3 ? 53 : 52
}
let dateRanges = [
['01-01-2020', '03-01-2020'], //week 1
['03-01-2020', '06-01-2020'], //week 1 and 2
['09-01-2020', '09-01-2020'], //week 2
['10-01-2020', '11-01-2020'], //week 2
['22-01-2020', '23-01-2020'], //week 4
];
let weeks = dateRanges.map(s => s.map(d => this.getWeek(d)));
let distinctWeeks = weeks.map(s => s.filter((item, pos) => s.indexOf(item) == pos)).flatMap(s => s);
let personPerWeek = distinctWeeks.reduce((a, c)=> {
a[c] = a[c] || 0;
a[c] += 1;
return a;
},{})
const weekCount = getISOWeeks(2020);
let personsPerWeeks = [];
getWorkWeeks = (workWeeks, weekCount) => {
for (let index = 1; index <= weekCount; index++) {
let personCount = personPerWeek[index] || 0;
workWeeks.push(personCount);
}
return workWeeks;
}
getWorkWeeks(personsPerWeeks, weekCount);
console.log(personsPerWeeks)
TA贡献1789条经验 获得超10个赞
检查这是否适合您。
dateRanges = [
["01-01-2020", "03-01-2020"], //week 1
["03-01-2020", "06-01-2020"], //week 1 and 2
["09-01-2020", "09-01-2020"], //week 2
["10-01-2020", "11-01-2020"], //week 2
["22-01-2020", "23-01-2020"], //week 4
];
mainArray = ['01-01-2020','31-12-2020'];
//console.log(dateRanges);
function diff_weeks(dt2, dt1)
{
var diff =(dt2.getTime() - dt1.getTime()) / 1000;
diff /= (60 * 60 * 24 * 7);
return Math.abs(Math.round(diff));
}
//for(var i = 0; i< dateRanges.length;i++){
//console.log(dateRanges[i][0]);
var dateString1 = mainArray[0];
var dateParts1 = dateString1.split("-");
var dt1 = new Date(+dateParts1[2], dateParts1[1] - 1, +dateParts1[0]);
var dateString2 = mainArray[1];
var dateParts2 = dateString2.split("-");
var dt2 = new Date(+dateParts2[2], dateParts2[1] - 1, +dateParts2[0]);
//console.log(dt1);
console.log(diff_weeks(dt1, dt2) + " Weeks");
//}
添加回答
举报