Vue格式化时间(昨天、本周、上周)

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"//上个月最后一天

 

 
上一篇:2021-12-03_学习B站Java视频Calendar对象_巴什博弈


下一篇:Java 时间 日 周 月 季 年