为了账号安全,请及时绑定邮箱和手机立即绑定

如何在JavaScript中的日期范围之间获取重复的星期日期计数?

如何在JavaScript中的日期范围之间获取重复的星期日期计数?

繁花不似锦 2022-09-23 21:45:43

我有一系列的日期范围,我需要知道全年每周有多少人工作。


Eg: mainArray = ['01-01-2020','31-12-2020']; //year range


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

  ....

];


//first we need to find all the weeks from the mainArray date-range 


//then calculate the weeks colliding in the dateRanges array.


the output should be =>


workLoadInWeeks = [2,3,0,1,0,0,0,0,......,0];

说明:由于 03-01-2020(第 1 周中的日期)在第 1 和第 2 个数组索引中重复,这就是为什么输出将 2 作为第一个值的原因。


由于第 2 周的日期在日期范围[1]、日期范围[2]、日期范围[3]中重复,这就是为什么输出中 3 作为第二个值的原因。


由于没有人在第3周工作,它在输出数组中为0


周开始 - 周日,每周7天,

我希望周的日期范围从1月1日开始,所以第一周将是部分周,因为第一周是星期三。


这听起来可能令人困惑。我已经尽力解释。


查看完整描述

2 回答

?
波斯汪

TA贡献1487条经验 获得超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)


查看完整回答
反对 回复 6天前
?
至尊宝的传说

TA贡献1450条经验 获得超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");

//}


查看完整回答
反对 回复 6天前
  • 2 回答
  • 0 关注
  • 9 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信