Advertisement

vue通过年-月-日,年-月,年搜索数据

阅读量:

无需多言,
直奔主题。
由于本次搜索涉及昨日、上月、本月以及当前年度的数据,
请确保已准备好并记录好相关日期信息。
具体信息如下:

复制代码
    function getDate() {
    var lastMonth = new Date(
      new Date().getFullYear(),
      new Date().getMonth() - 1,
      new Date().getDate()
    ); // 获取上个月日期
    var currentMonth = new Date().getMonth() + 1; // 获取本月
    date.year = new Date().getFullYear(); // 获取本年
    date.lastYear = new Date().getFullYear() -1;
    date.lastMonth = lastMonth.toISOString().slice(0, 7); // 格式化上个月日期
    date.month = date.year + '-' + (currentMonth < 10 ? '0' + currentMonth : currentMonth); // 格式化本月日期
    var yesterday = new Date();
    yesterday.setDate(yesterday.getDate() - 1);
    date.yesterday = yesterday.toISOString().slice(0, 10);
    console.log('昨日' + date.yesterday);
    console.log('上月:' + date.lastMonth);
    console.log('本月:' + date.month);
    console.log('本年:' + date.year);
    console.log('去年'+ date.lastYear);;
      }
    
    
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
    
    AI写代码

date是我已经定义好的一个reactive

复制代码
      const date = reactive({
    yesterday: '',
    lastMonth: '',
    month: '',
    year: '',
    lastYear:''
      });
    
    
      
      
      
      
      
      
      
    
    AI写代码

开始进行搜索流程
data_date这一字段始终遵循yyyy-mm-dd的格式
首先专注于 yesterday 的数据
例如:昨天的数据是 2024-02-27 ,其变量命名为 date.yesterday
这个数组即为目标要进行搜索的部分

复制代码
     let yeArr = arr.filter((item) => item.data_date == date.yesterday);
     //根据自己的date.yesterday与数组中的data_date进行比较,把相等的筛选出来
     //此时这个yeArr就是筛选后的数组
    
    
      
      
      
    
    AI写代码

随后是搜索过去一个月的数据,变量名设置为date.lastMonth的同时要求与arr中的每一项目data_date对应匹配

复制代码
    var yearMonth = date.lastMonth.split('-').map(Number);
    var filteredDateObjects = arr.filter(function (obj) {
      var dateYearMonth = obj.data_date.split('-').slice(0, 2).map(Number);
      return dateYearMonth[0] === yearMonth[0] && dateYearMonth[1] === yearMonth[1];
    });
     //最后搜索到的数据为filteredDateObjects
    
    
      
      
      
      
      
      
    
    AI写代码

3,本月

复制代码
    var nowMonth = date.month.split('-').map(Number);
    var filteredDateObjects = arr.filter(function (obj) {
      var dateYearMonth = obj.data_date.split('-').slice(0, 2).map(Number);
      return dateYearMonth[0] === nowMonth[0] && dateYearMonth[1] === nowMonth[1];
    });
    //最后搜索到的数据为filteredDateObjects
    
    
      
      
      
      
      
      
    
    AI写代码

4,本年

复制代码
     var testYear = arr.filter(function (obj) {
      var objYear = obj.data_date.split('-')[0];
      return objYear == date.year;
      //这个date.year就是我们当前的年份
    });
    
    
      
      
      
      
      
    
    AI写代码

5,去年
去年跟今年同理

复制代码
     var lastsYear = arr.filter(function (obj) {
      var objYear = obj.data_date.split('-')[0];
      return objYear == date.lastYear; 
    });
    
    
      
      
      
      
    
    AI写代码

over,有用的话留下个免费的双击关注吧~

全部评论 (0)

还没有任何评论哟~