需求:获取某月的第一天或者最后一天,这里给出的函数是日月年通用
handlerTime(val, type) {
// 当月---当天 默认当天, 非当月--月的最后一天 年 当天, 年最后一天 前7日当天 总 当天
let infoDate = '';
const format =
type == 'day' ? 'YYYY-MM-DD' : type == 'month' ? 'YYYY-MM' : 'YYYY';
const curtDate = moment().format(format);
const defaultDate = moment(val).format(format);
if(type == 'day') {
infoDate = moment(val).format('YYYY-MM-DD');
}
else if(type == 'month') {
infoDate =
defaultDate == curtDate
? moment().format('YYYY-MM') + '-01'
: moment(val).endOf('months').format('YYYY-MM-DD');
}
else {
infoDate =
defaultDate == curtDate
? moment().format('YYYY-MM') + '-01'
: moment(val).endOf('year').format('YYYY-MM-DD');
}
return infoDate;
},
//日的掉用
this.handlerTime(moment(new Date()).format('YYYY-MM-DD'), 'day');
//月的掉用
this.handlerTime(moment(new Date()).format('YYYY-MM'), 'month');
//年的掉用
this.handlerTime(moment(new Date()).format('YYYY'), 'year');