前端近7天,近半个月,近1个月,近1年的日期处理

前端如何获取近7天,近1年的日期进行查询?

methods:{
 //近7天
    getRangeDate(ranges) {
      let nowDays = new Date();
      let getYear = nowDays.getFullYear();
      let getMonth = nowDays.getMonth() + 1;
      let getDate = nowDays.getDate();
      let nd = new Date();
      nd = nd.valueOf();
      nd = nd - ranges * 24 * 60 * 60 * 1000;
      nd = new Date(nd);
      let lastYear = nd.getFullYear();
      let lastMonth = nd.getMonth() + 1;
      let lastDate = nd.getDate();
      if (getMonth === 0) {
        getMonth = '12';
        getYear = new Date().getFullYear() - 1;
      } else {
        getYear = new Date().getFullYear();
      }
      if (getMonth < 10) {
        getMonth = '0' + getMonth;
      }
      if (getDate < 10) {
        getDate = '0' + getDate;
      }
      if (lastMonth < 10) {
        lastMonth = '0' + lastMonth;
      }
      if (lastDate < 10) {
        lastDate = '0' + lastDate;
      }
      let nowTime = getYear + '' + getMonth + '' + getDate;
      console.log('当前日期', getYear, getMonth, getDate);//2024 04 19
      let lastTime = '';
      lastTime = lastYear + '' + lastMonth + '' + lastDate;
      console.log('近7天', lastTime);//20240412
      let doubleTime = {
        nowTime: nowTime,
        lastTime: lastTime,
      };
      return doubleTime;
    },
    
//近1年
isOneYear() {
      let year = new Date().getFullYear();
      let lastYear = new Date().getFullYear() - 1;
      let month = new Date().getMonth() + 1;
      let getDate = new Date().getDate();
      if (month < 10) {
        month = '0' + month;
      }
      if (getDate < 10) {
        getDate = '0' + getDate;
      }
      const nowDate =
        year.toString() + '/' + month.toString() + '/' + getDate.toString();
      let lastDate =
        lastYear.toString() + '/' + month.toString() + '/' + getDate.toString();
      console.log('isOneYear', nowDate, lastDate);
      //2024/04/19 2023/04/19
      let nowTime = new Date(nowDate).getTime();
      let lastTime = new Date(lastDate).getTime();
      let doubleTime = {
        nowTime: nowTime,
        lastTime: lastTime,
      };
      return doubleTime;
    },
}
created(){
	this.getRangeDate(7);
	this.isOneYear();
}

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-04-20 12:44:04       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-04-20 12:44:04       100 阅读
  3. 在Django里面运行非项目文件

    2024-04-20 12:44:04       82 阅读
  4. Python语言-面向对象

    2024-04-20 12:44:04       91 阅读

热门阅读

  1. 笔记:Python猴子吃桃

    2024-04-20 12:44:04       36 阅读
  2. C#使用ftp进行文件上传和下载功能

    2024-04-20 12:44:04       35 阅读
  3. 第二十章hive

    2024-04-20 12:44:04       33 阅读
  4. Python网络爬虫项目开发实战:如何处理动态内容

    2024-04-20 12:44:04       40 阅读
  5. React中子传父的方式及原理

    2024-04-20 12:44:04       34 阅读
  6. postgreSQL学习指南(基础)

    2024-04-20 12:44:04       41 阅读
  7. Spring boot注解开发mybatis

    2024-04-20 12:44:04       34 阅读
  8. python-基础(2)-数值运算

    2024-04-20 12:44:04       30 阅读
  9. 基于Python的招聘信息爬虫系统的设计与实现

    2024-04-20 12:44:04       40 阅读
  10. 基于K-prototype算法聚类

    2024-04-20 12:44:04       35 阅读
  11. 【备忘】利用FFMpeg读取视频第一帧作为封面

    2024-04-20 12:44:04       43 阅读
  12. Vue3页面的执行过程

    2024-04-20 12:44:04       38 阅读