vue獲取當天、一周、一個月的時間


//獲取時間
    getTime() {
      var getNowDate = new Date();
      //獲取當前日期時間
      const nowYear = getNowDate.getFullYear();
      let nowMonth = getNowDate.getMonth() + 1;
      let nowDay = getNowDate.getDate();
      if (nowMonth >= 1 && nowMonth <= 9) {
        nowMonth = "0" + nowMonth;
      }
      if (nowDay >= 1 && nowDay <= 9) {
        nowDay = "0" + nowDay;
      } else if (nowDay == 1) {
        nowDay = 30;
        nowMonth -= 1;
      }
      var nowDate = nowYear + "-" + nowMonth + "-" + nowDay;
      //獲取一周時間
      var getOneweek = new Date(getNowDate - 7 * 24 * 3600 * 1000);
      var thYear = getOneweek.getFullYear();
      var thMonth = getOneweek.getMonth() + 1;
      var thDay = getOneweek.getDate();
      if (thMonth >= 1 && thMonth <= 9) {
        thMonth = "0" + thMonth;
      }
      if (thDay >= 0 && thDay <= 9) {
        thDay = "0" + thDay;
      }
      var oneweekDate = thYear + "-" + thMonth + "-" + thDay;
      //獲取一個月前時間
      getNowDate.setMonth(getNowDate.getMonth() - 1);
      var thirtyYear = getNowDate.getFullYear();
      var thirtyMonth = getNowDate.getMonth() + 1;
      var thirtyDay = getNowDate.getDate();
      if (thirtyMonth >= 1 && thirtyMonth <= 9) {
        thirtyMonth = "0" + thirtyMonth;
      }
      if (thirtyDay >= 1 && thirtyDay <= 9) {
        thirtyDay = "0" + thirtyDay;
      }
      var monthDate = thirtyYear + "-" + thirtyMonth + "-" + thirtyDay;
      // 獲取三年時間
      var date = new Date();
      var y = date.getFullYear() - 2; // 減一是算去年時間,+ 1 是明年時間
      var dateStr = y + "-" + 1 + "-" + 1;
      return {
        nowDate: nowDate,
        oneweekDate: oneweekDate,
        monthDate: monthDate,
        dateStr: dateStr,
      };
    },

  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM