import moment from 'moment'
const STANTFORMAT = "YYYY-MM-DD 00:00:00"
const ENDFORMAT = "YYYY-MM-DD 23:59:59"
/**
* @description:
* @param {String} type
* ['前日','昨日', '今日', '明日', '上周', '本周', '下周', '上月', '本月', '下月',
* '去年', '今年', '明年', '上季度', '本季度', '下季度']
* @return {Object} {dateFrom, dateTo}
*/
export function getTimeRange(type) {
let dateFrom = '', dateTo = ''
switch (type) {
case '前日':
dateFrom = moment().subtract(2, "days").format(STANTFORMAT)
dateTo = moment().subtract(2, "days").format(ENDFORMAT)
break;
case '昨日':
dateFrom = moment().subtract(1, "days").format(STANTFORMAT)
dateTo = moment().subtract(1, "days").format(ENDFORMAT)
break;
case '今日':
dateFrom = moment().format(STANTFORMAT)
dateTo = moment().format(ENDFORMAT)
break;
case '明日':
dateFrom = moment().subtract(-1, 'days').format(STANTFORMAT)
dateTo = moment().subtract(-1, 'days').format(ENDFORMAT)
break;
case '上周':
dateFrom = moment().day(-6).format(STANTFORMAT)
dateTo = moment().day(0).format(ENDFORMAT)
break;
case '本周':
dateFrom = moment().day(1).format(STANTFORMAT)
dateTo = moment().day(7).format(ENDFORMAT)
break;
case '下周':
dateFrom = moment().startOf('week').subtract(-1, 'week').format(STANTFORMAT)
dateTo = moment().endOf('week').subtract(-1, 'week').format(ENDFORMAT)
break;
case '上月':
dateFrom = moment(new Date()).subtract(1, 'months').startOf('month').format(STANTFORMAT);
dateTo = moment(new Date()).subtract(1, 'months').endOf('month').format(ENDFORMAT)
break;
case '本月':
dateFrom = moment().startOf("month").format(STANTFORMAT);
dateTo = moment().endOf("month").format(ENDFORMAT)
break;
case '下月':
dateFrom = moment().month(moment().month() + 1).startOf('month').format(STANTFORMAT);
dateTo = moment().month(moment().month() + 1).endOf('month').format(ENDFORMAT);
break;
case '去年':
dateFrom = moment(new Date()).subtract(1, 'year').startOf('year').format(STANTFORMAT);
dateTo = moment(new Date()).subtract(1, 'year').endOf('year').format(ENDFORMAT)
break;
case '今年':
dateFrom = moment().startOf('year').format(STANTFORMAT);
dateTo = moment().endOf('year').format(ENDFORMAT)
break;
case '明年':
dateFrom = moment().year(moment().year() + 1).startOf('year').format(STANTFORMAT);
dateTo = moment().year(moment().year() + 1).endOf('year').format(ENDFORMAT);
break;
case '上季度':
dateFrom = moment().quarter(moment().quarter() - 1).startOf('quarter').format(STANTFORMAT);
dateTo = moment().quarter(moment().quarter() - 1).endOf('quarter').format(ENDFORMAT);
break;
case '本季度':
dateFrom = moment().startOf('quarter').format(STANTFORMAT);
dateTo = moment().endOf('quarter').format(ENDFORMAT);
break;
case '下季度':
dateFrom = moment().quarter(moment().quarter() + 1).startOf('quarter').format(STANTFORMAT);
dateTo = moment().quarter(moment().quarter() + 1).endOf('quarter').format(ENDFORMAT);
break;
}
return { dateFrom, dateTo }
}
10-12
1164