Vue格式化时间,moment应该更简单吧。
  • 昨天
var now = new Date();
var yest = new Date();
yest.setDate(now.getDate() - 1)
var monthn = now.getMonth() + 1;
var yearn = now.getFullYear();
var dayn = now.getDate();
 
var monthnY = yest.getMonth() + 1;
var yearnY = yest.getFullYear();
var daynY = yest.getDate();
 
this.searchForm.jssj = yearn + "-" + monthn + "-" + dayn;
this.searchForm.kssj = yearnY + "-" + monthnY + "-" + daynY;
  • 本周
var now = new Date();
var nowTime = now.getTime();
var day = now.getDay()|| 7;
var oneDayTime = 24 * 60 * 60 * 1000;
 
var MondayTime = nowTime - (day - 1) * oneDayTime;
var monday = new Date(MondayTime);
 
var now = new Date();
var monthn = now.getMonth() + 1;
var yearn = now.getFullYear();
var dayn = now.getDate();
 
var monthnY = monday.getMonth() + 1;
var yearnY = monday.getFullYear();
var daynY = monday.getDate();
 
this.searchForm.jssj = yearn + "-" + monthn + "-" + dayn + " 23:59:59";
this.searchForm.kssj = yearnY + "-" + monthnY + "-" + daynY;
  • 上周
var now = new Date();
var nowTime = now.getTime();
var day = now.getDay()|| 7;
var oneDayTime = 24 * 60 * 60 * 1000;
 
var MondayTime = nowTime - (day - 1) * oneDayTime - 7 * oneDayTime;
var monday = new Date(MondayTime);
 
var SundayTime = nowTime + (7 - day) * oneDayTime - 7 * oneDayTime;
var sunday = new Date(SundayTime);
 
var now = new Date();
var monthn = sunday.getMonth() + 1;
var yearn = sunday.getFullYear();
var dayn = sunday.getDate();
 
var monthnY = monday.getMonth() + 1;
var yearnY = monday.getFullYear();
var daynY = monday.getDate();
 
this.searchForm.jssj = yearn + "-" + monthn + "-" + dayn + " 23:59:59";
this.searchForm.kssj = yearnY + "-" + monthnY + "-" + daynY;
  •  上月
        var nowdays = new Date();
        var year = nowdays.getFullYear();
        var month = nowdays.getMonth();
        if (month == 0) {
          month = 12;
          year = year - 1;

        }
        if (month < 10) {
          month = '0' + month;
        }

        var myDate = new Date(year, month, 0);

        this.searchForm.kssj = year + '-' + month + '-01'; //上个月第一天
        this.searchForm.jssj = year + '-' + month + '-' + myDate.getDate() + " 23:59:59"//上个月最后一天

 补充moment:

      switch (type){
     //今天
case 1: this.searchForm.kssj = moment().startOf('day').format('YYYY-MM-DD HH:mm:ss'); this.searchForm.jssj = moment().endOf('day').format('YYYY-MM-DD HH:mm:ss'); break;
    //昨天
case 2: this.searchForm.kssj = moment().subtract(1, "days").startOf('day').format('YYYY-MM-DD HH:mm:ss'); this.searchForm.jssj = moment().subtract(1, "days").endOf('day').format('YYYY-MM-DD HH:mm:ss'); break;
     //本周
case 3: this.searchForm.kssj = moment().day("Monday").format('YYYY-MM-DD 00:00:00'); this.searchForm.jssj = moment().endOf('day').format('YYYY-MM-DD HH:mm:ss'); break;
    //上周
case 4: this.searchForm.kssj = moment().subtract(7, "days").day("Monday").format('YYYY-MM-DD 00:00:00'); this.searchForm.jssj = moment().subtract(7, "days").day("Monday").subtract(-6, "days").format( 'YYYY-MM-DD 23:59:59'); break;
    //上月
case 5: let day = moment().startOf('month').subtract(1, "days") this.searchForm.kssj = day.startOf('month').format('YYYY-MM-DD HH:mm:ss'); this.searchForm.jssj = day.endOf('month').format('YYYY-MM-DD HH:mm:ss'); break; }