首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何根据javascript中的日期填充周数

如何根据javascript中的日期填充周数
EN

Stack Overflow用户
提问于 2020-05-02 16:36:58
回答 2查看 63关注 0票数 0

我有如下数据:

代码语言:javascript
运行
复制
************************************************************************************************

                                                    May - 2020


                    Date                                          Week                 Week Count

Fri May 01 2020 00:00:00 GMT+0500 (Pakistan Standard Time)             18                   1   
Sat May 02 2020 00:00:00 GMT+0500 (Pakistan Standard Time)             18                   1   
Sun May 03 2020 00:00:00 GMT+0500 (Pakistan Standard Time)             18                   1+2 = 3  
Mon May 04 2020 00:00:00 GMT+0500 (Pakistan Standard Time)              19                  1
Tue May 05 2020 00:00:00 GMT+0500 (Pakistan Standard Time)              19                  1
Wed May 06 2020 00:00:00 GMT+0500 (Pakistan Standard Time)              19                  1
Thu May 07 2020 00:00:00 GMT+0500 (Pakistan Standard Time)              19                  1
Fri May 08 2020 00:00:00 GMT+0500 (Pakistan Standard Time)              19                  1
Sat May 09 2020 00:00:00 GMT+0500 (Pakistan Standard Time)              19                  1
Sun May 10 2020 00:00:00 GMT+0500 (Pakistan Standard Time)              19                  1+6 = 7
Mon May 11 2020 00:00:00 GMT+0500 (Pakistan Standard Time)              20                  1
Tue May 12 2020 00:00:00 GMT+0500 (Pakistan Standard Time)              20                  1
Wed May 13 2020 00:00:00 GMT+0500 (Pakistan Standard Time)              20                  1
Thu May 14 2020 00:00:00 GMT+0500 (Pakistan Standard Time)              20                  1
Fri May 15 2020 00:00:00 GMT+0500 (Pakistan Standard Time)              20                  1
Sat May 16 2020 00:00:00 GMT+0500 (Pakistan Standard Time)              20                  1
Sun May 17 2020 00:00:00 GMT+0500 (Pakistan Standard Time)              20                  1+6 = 7
Mon May 18 2020 00:00:00 GMT+0500 (Pakistan Standard Time)              21                  1
Tue May 19 2020 00:00:00 GMT+0500 (Pakistan Standard Time)              21                  1
Wed May 20 2020 00:00:00 GMT+0500 (Pakistan Standard Time)              21                  1
Thu May 21 2020 00:00:00 GMT+0500 (Pakistan Standard Time)              21                  1
Fri May 22 2020 00:00:00 GMT+0500 (Pakistan Standard Time)              21                  1
Sat May 23 2020 00:00:00 GMT+0500 (Pakistan Standard Time)              21                  1
Sun May 24 2020 00:00:00 GMT+0500 (Pakistan Standard Time)              21                  1+6 = 7 
Mon May 25 2020 00:00:00 GMT+0500 (Pakistan Standard Time)              22                  1
Tue May 26 2020 00:00:00 GMT+0500 (Pakistan Standard Time)              22                  1
Wed May 27 2020 00:00:00 GMT+0500 (Pakistan Standard Time)              22                  1
Thu May 28 2020 00:00:00 GMT+0500 (Pakistan Standard Time)              22                  1
Fri May 29 2020 00:00:00 GMT+0500 (Pakistan Standard Time)              22                  1
Sat May 30 2020 00:00:00 GMT+0500 (Pakistan Standard Time)              22                  1
Sun May 31 2020 00:00:00 GMT+0500 (Pakistan Standard Time)              22                  1+6 = 7

***********************************************************************************************

我喜欢根据每周发生的情况来安排它:所以最终的结果是:

代码语言:javascript
运行
复制
18=>3, 19=>7, 20=> 7, 21=>7, 22=>7

因此,在5月份,第18周出现了1次,但在2020年5月的第18周,有3个日期被占用。同样,上述其他日期也是如此。

我正在尝试的代码是:

代码语言:javascript
运行
复制
{
  daysNumber.map((number, index) => {
    let d = number.split('-');
    NDate = new Date(d[0], d[1] - 1, d[2]);
    weekNum = this.getWeekNumber(NDate);
  })
}

getWeekNumber(now) {
  let onejan = new Date(now.getFullYear(), 0, 1);
  return Math.ceil((((now - onejan) / 86400000) + onejan.getDay()) / 7);
}

getDaysNumber(year, month) {
        const dates = [];
        const daysInMonth = new Date(year, month, 0).getDate();
        for (let i = 1; i <= daysInMonth; i++) {
            let parts = new Date(year + "-" + month + "-" + i);
            dates.push(this.convertToDesiredDate(parts));
        }
        return dates;
    }

convertToDesiredDate(str) {
        let date = new Date(str),
            month = ("0" + (date.getMonth() + 1)).slice(-2),
            day = ("0" + date.getDate()).slice(-2);
        return [date.getFullYear(), month, day].join("-");
    }

其中daysNumber包含从01-31 (5月)开始的月日。

它会增长取决于月份。

我不得不将它们作为数组填充,以确定每周有多少次出现在像我的输出这样的日期。

EN

Stack Overflow用户

发布于 2020-05-02 16:55:24

你的问题有点乱,但我会尽力的。我使用了您的getWeekNumber实现,提供了自己的日期,并编写了一个简单的reduce。如果某一周没有条目,我们将其加为1,否则就会增加,仅此而已。了解更多关于Array.prototype.reduce 这里的信息。

代码语言:javascript
运行
复制
const dates = [
  new Date('2000-01-01'),
  new Date('2000-01-25'),
  new Date('2000-02-01'),
  new Date('2000-02-02'),
  new Date('2000-02-03'),
];

const getWeekNumber = now => {
  let onejan = new Date(now.getFullYear(), 0, 1);
  return Math.ceil((((now - onejan) / 86400000) + onejan.getDay()) / 7);
}

const result = dates.reduce((countPerWeek, date) => {
  const weekNumber = getWeekNumber(date);

  if (!countPerWeek[weekNumber]) {
    countPerWeek[weekNumber] = 1;
  } else {
    countPerWeek[weekNumber]++;
  }
 
  return countPerWeek;
}, {});

console.log(result);

票数 2
EN
查看全部 2 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61562844

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档