首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >使用momentjs从范围内获取数月和数周

使用momentjs从范围内获取数月和数周
EN

Stack Overflow用户
提问于 2019-03-04 21:20:50
回答 1查看 55关注 0票数 0

如果我有类似这样的东西:

代码语言:javascript
复制
let start = moment('2019-01-27');
let end = moment('2019-02-28');

我如何才能得到真正的月份开始和结束以及星期开始和结束,这样我就可以计算这些周和月内的一些数据。我平日里有这个:

代码语言:javascript
复制
getWeekdays(data, labels) {
        let start = moment(this.$store.state.labels[0]);
        let end = moment(this.$store.state.labels[this.$store.state.labels.length - 1]);
        let new_labels = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday'];
        let store_labels = this.$store.state.labels;
        let store_data = this.$store.state.data;
        let new_data = [];
        for(let i=1; i<8; i++) {
            var arr = [];
            let tmp = start.clone().day(i);
            if( tmp.isAfter(start, 'd') ){
              arr.push(tmp.format('YYYY-MM-DD'));
            }
            while( tmp.isBefore(end) ){
              tmp.add(7, 'days');
              arr.push(tmp.format('YYYY-MM-DD'));
            }
            arr.pop();
            let count = 0;
            _.forEach(arr, function(val) {
                let key = store_labels.findIndex(function(i){return i === val});
                count = count + store_data[key];
            });
            new_data.push(count);
        }
        console.log(new_data);
        return {data: new_data, labels: new_labels};
      }

data = [1,2,5,3,8]labels = ['2019-01-27','2019-01-28','2019-01-29','2019-01-30','2019-01-31']在哪里工作,但我不确定如何做到这一点几个月甚至几个星期。

EN

回答 1

Stack Overflow用户

发布于 2019-03-04 22:40:17

这是几个月来的答案,如果有人需要:D

代码语言:javascript
复制
let new_range = {};
        _.forEach(labels, function(value, key) {
            new_range[moment(value).format('YYYY MM')] = [];
        });
        _.forEach(labels, function(value, key) {
            new_range[moment(value).format('YYYY MM')].push(data[key]);
        });
        let new_labels = Object.keys(new_range);
        let new_data = [];
        _.forEach(new_range, function(value, key) {
            new_data.push(_.sumBy(value, function(n) {return n;}));
        });

        return {data: new_data, labels: new_labels};

在一整天之后,这里是一个真正的星期:

代码语言:javascript
复制
const range = ['2019-01-22', '2019-01-23', '2019-01-24', '2019-01-25', '2019-01-26', '2019-01-27', '2019-01-28', '2019-01-29', '2019-01-30', '2019-01-31', '2019-02-01', '2019-02-02', '2019-02-03', '2019-02-04', '2019-02-05', '2019-02-06', '2019-02-07', '2019-02-08', '2019-02-09', '2019-02-10', '2019-02-11', '2019-02-12', '2019-02-13', '2019-02-14', '2019-02-15', '2019-02-16', '2019-02-17', '2019-02-18', '2019-02-19', '2019-02-20', '2019-02-21', '2019-02-22', '2019-02-23', '2019-02-24', '2019-02-25', '2019-02-26', '2019-02-27', '2019-02-28', '2019-03-01'];
const backup = ['2019-01-22', '2019-01-23', '2019-01-24', '2019-01-25', '2019-01-26', '2019-01-27', '2019-01-28', '2019-01-29', '2019-01-30', '2019-01-31', '2019-02-01', '2019-02-02', '2019-02-03', '2019-02-04', '2019-02-05', '2019-02-06', '2019-02-07', '2019-02-08', '2019-02-09', '2019-02-10', '2019-02-11', '2019-02-12', '2019-02-13', '2019-02-14', '2019-02-15', '2019-02-16', '2019-02-17', '2019-02-18', '2019-02-19', '2019-02-20', '2019-02-21', '2019-02-22', '2019-02-23', '2019-02-24', '2019-02-25', '2019-02-26', '2019-02-27', '2019-02-28', '2019-03-01'];
const data = [5,2,3,1,5,5,2,3,1,5,5,2,3,1,5,5,2,3,1,5,5,2,3,1,5,5,2,3,1,5,5,2,3,1,5,5,2,3,1];
console.log(bindData(data, backup, getWeeks(range)));


function bindData(data, range, labels) {
    let new_data = {};
    Object.keys(labels).forEach(function(val) {
    let count = 0;
    for(let j = 0; j<labels[val].length; j++) {
        for(let i = 0; i<range.length; i++) {
        if(labels[val][j] == range[i]) {
            count = count + data[i];
        }
      }
    }
    new_data[val] = count;
  });
  return new_data;
}

function getWeeks(labels) {

    let start = moment(labels[0]);
  let new_range = {};
  var arr = [];
  let tmp = start.clone().day(7);
  let sunday = tmp.format('YYYY-MM-DD');
  let sunday_index = labels.indexOf(sunday);
  if(sunday_index === -1) {
    new_range[sunday] = labels;
  }else{
    let first_week = labels.slice(0, sunday_index + 1);
    new_range[first_week[first_week.length - 1]] = first_week;
    labels.splice(0, sunday_index + 1);
    let last_days = labels.length % 7;
    if(last_days != 0) {
      let last_week = labels.splice(labels.length - last_days, last_days);
      new_range[last_week[last_week.length - 1]] = last_week;
    }
    for(let i = 0; i<labels.length; i++) {
    let a = 0;
      new_range[labels[6]] = labels.splice(a, 7);
    }

  }
    return new_range;
}

现在,由于某些原因,我需要有双标签数组,如果我使用了其中的一个,我将在bindData函数中得到空数组。如果你知道为什么。编辑它。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54984188

复制
相关文章

相似问题

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