獲取當前日期時間,獲取昨天日期,獲取上周日期,獲取上月日期,獲取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()}`;
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容