天天看點

擷取目前日期時間,擷取昨天日期,擷取上周日期,擷取上月日期,擷取30天前日期

Vue中

data() {
    return {
      /*目前日期時間*/
      nowYear: '',
      nowMonth: '',
      nowDate: '',
      nowDay: '',
      nowHours: '',
      nowMinutes: '',
      nowSeconds: '',
      turnoverTime: ''
    };
  },
           

擷取目前日期時間

// 擷取目前日期時間
    getNowTime() {
      this.nowYear = new Date().getFullYear();
      this.nowMonth = new Date().getMonth()+1;
      this.nowDate = new Date().getDate();
      this.nowDay = new Date().getDay();
      this.nowHours = new Date().getHours();
      this.nowMinutes = new Date().getMinutes()<10 ? '0'+new Date().getMinutes() : new Date().getMinutes();
      this.nowSeconds = new Date().getSeconds()<10 ? '0'+new Date().getSeconds() : new Date().getSeconds();
      this.turnoverTime = this.nowYear+'年'+this.nowMonth+'月'+this.nowDate+'日'+' '+this.nowHours+':'+this.nowMinutes+':'+this.nowSeconds;
    },
           

擷取上周日期

let thisWeekStart; //本周周一的時間
        if (this.nowDay === 0) {  //周天的情況;
          thisWeekStart = (new Date(this.nowYear + '-' + this.nowMonth + '-' + this.nowDate)).getTime() - ((this.nowDay) + 6) * 86400000;
        } else {
          thisWeekStart = (new Date(this.nowYear + '-' + this.nowMonth + '-' + this.nowDate)).getTime() - ((this.nowDay) - 1) * 86400000;
        }
        //獲得上周時間
        var prevWeekStart = thisWeekStart - 7 * 86400000;//上周周一的時間
        var prevWeekEnd = thisWeekStart - 1 * 86400000;//上周周日的時間
        let sTime = this.formatDate(new Date(prevWeekStart)); //開始時間
        let eTime = this.formatDate(new Date(prevWeekEnd));   //結束時間
           

擷取上月日期

var prevCurrentYear = 0, prevCurrentMonth = 0;
        if (this.nowMonth === 1) {
          prevCurrentYear = this.nowYear - 1;
          prevCurrentMonth = 12;
        } else {
          prevCurrentYear = this.nowYear;
          prevCurrentMonth = this.nowMonth - 2;
        }
        //獲得上月時間
        var prevmonthLastday = (new Date(this.nowYear, this.nowMonth-1, 1)).getTime() - 86400000;
        let sTime = this.formatDate(new Date(prevCurrentYear, prevCurrentMonth, 1));  //開始時間
        let eTime = this.formatDate(new Date(prevmonthLastday));   //結束時間
           

擷取昨天日期

let date1 = new Date();
let date2 = new Date(date1);
date2.setDate(date1.getDate() - 1);
`${date2.getFullYear()}-${date2.getMonth() + 1<10?`0${date2.getMonth() + 1}`:date2.getMonth() + 1}-${date2.getDate()}`;
           

擷取30天前日期

let date1 = new Date();
let date2 = new Date(date1);
date2.setDate(date1.getDate() - 30);
`${date2.getFullYear()}-${date2.getMonth() + 1<10?`0${date2.getMonth() + 1}`:date2.getMonth() + 1}-${date2.getDate()}`;
           

繼續閱讀