使用Moment.js从范围中获取几个月和几周

时间:2019-03-04 13:20:50

标签: javascript date vuejs2 momentjs

如果我有类似的东西:

let start = moment('2019-01-27');
let end = moment('2019-02-28');

我如何才能获得真实的月开始和结束时间以及几周的开始和结束时间,以便我可以计算出那几周和几个月内的一些数据。 我在工作日有这个

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']都可以正常工作的地方,但是我不确定如何在几个月和几周内做到这一点。

1 个答案:

答案 0 :(得分:0)

这是几个月的答案,即使有人需要:D

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};

整天过后,这是真正的一周:

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函数中获得空数组。如果你知道为什么。编辑它。