vue获取当天、一周、一个月的时间

67 篇文章 1 订阅
//获取时间
    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,
      };
    },
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值