index.js 3.65 KB
Newer Older
涂茜's avatar
涂茜 committed
1 2 3 4
import moment from 'moment';

// 根据设定查询条件 返回时间范围
const getTimeRange = (type) => {
涂茜's avatar
涂茜 committed
5 6
  let start = '';
  let end = '';
涂茜's avatar
涂茜 committed
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
  switch (type) {
    case 'thisYear': // 今年
      start = moment().startOf('year');
      end = moment().endOf('year');
      break;
    case 'lastYear': // 去年
      start = moment()
        .year(moment().year() - 1)
        .startOf('year');
      end = moment()
        .year(moment().year() - 1)
        .endOf('year');
      break;
    case 'nextYear': // 明年
      start = moment()
        .year(moment().year() + 1)
        .startOf('year');
      end = moment()
        .year(moment().year() + 1)
        .endOf('year');
      break;
    case 'lastQuarter': // 上季度
      start = moment()
        .quarter(moment().quarter() - 1)
        .startOf('quarter');
      end = moment()
        .quarter(moment().quarter() - 1)
        .endOf('quarter');
      break;
    case 'thisQuarter': // 本季度
      start = moment().startOf('quarter');
      end = moment().endOf('quarter');
      break;
    case 'nextQuarter': // 下季度
      start = moment()
        .quarter(moment().quarter() + 1)
        .startOf('quarter');
      end = moment()
        .quarter(moment().quarter() + 1)
        .endOf('quarter');
      break;
    case 'lastMonth': // 上月
      start = moment()
        .month(moment().month() - 1)
        .startOf('month');
      end = moment()
        .month(moment().month() - 1)
        .endOf('month');
      break;
    case 'thisMonth': // 本月
      start = moment().startOf('month');
      end = moment().endOf('month');
      break;
    case 'nextMonth': // 下月
      start = moment()
        .month(moment().month() + 1)
        .startOf('month');
      end = moment()
        .month(moment().month() + 1)
        .endOf('month');
      break;
    case 'lastWeek': // 上周
涂茜's avatar
涂茜 committed
69 70
      start = moment().startOf('week').subtract(1, 'week');
      end = moment().endOf('week').subtract(1, 'week');
涂茜's avatar
涂茜 committed
71 72
      break;
    case 'thisWeek': // 本周
涂茜's avatar
涂茜 committed
73 74
      start = moment().startOf('week');
      end = moment().endOf('week');
涂茜's avatar
涂茜 committed
75 76
      break;
    case 'nextWeek': // 下周
涂茜's avatar
涂茜 committed
77 78
      start = moment().startOf('week').subtract(-1, 'week');
      end = moment().endOf('week').subtract(-1, 'week');
涂茜's avatar
涂茜 committed
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
      break;
    case 'lastDay': // 昨天
      start = moment().subtract(1, 'days');
      end = moment().subtract(1, 'days');
      break;
    case 'thisDay': // 今天
      start = moment();
      end = moment();
      break;
    case 'nextDay': // 明天
      start = moment().subtract(-1, 'days');
      end = moment().subtract(-1, 'days');
      break;
    default:
      start = moment();
      end = moment();
      break;
  }
  if (!!start && !!end) {
    start = start.format(startFormat);
    end = end.format(endFormat);
  }
  return { startTime: start, endTime: end };
};

const getRange = (date, type) => {
涂茜's avatar
涂茜 committed
105 106
  let start = '';
  let end = '';
涂茜's avatar
涂茜 committed
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
  switch (type) {
    case 'day':
      start = moment(date);
      end = moment(date);
      break;
    case 'week':
      start = moment(date).startOf('week');
      end = moment(date).endOf('week');
      break;
    case 'month':
      start = moment(date).startOf('month');
      end = moment(date).endOf('month');
      break;
    case 'year':
      start = moment(date).startOf('year');
      end = moment(date).endOf('year');
      break;
    default:
      start = moment(date);
      end = moment(date);
      break;
  }
  if (!!start && !!end) {
    start = start.format(startFormat);
    end = end.format(endFormat);
  }
  return { startTime: start, endTime: end };
};

export { getTimeRange, getRange };

const startFormat = 'YYYY-MM-DD 00:00:00';
const endFormat = 'YYYY-MM-DD 23:59:59';