开发需求总结19-vue 根据后端返回一年的数据,过滤出符合条件数据

发布于:2024-11-29 ⋅ 阅读:(10) ⋅ 点赞:(0)

需求描述:

定义时间分界点:每月26号8点,过了26号8点则过滤出data数组中符合条件数据下个月的数据,否则过滤出当月数据

1.假如现在是2024年11月14日,那么过滤出data数组中日期都是2024-11月的数据;

2.假如现在是2024年11月26号8点之后,那么就过滤出data数组中11月之后下个月的数据,也就是12月的数据。

后端返回数据:

返回如下格式,过滤出符合需求条件的数据

let data = [
    {month: '2023-12', name: '111'}, 
    {month: '2023-12', name: '111'}, 
    {month: '2023-12', name: '111'}, 
    {month: '2024-12', name: '111'},
    {month: '2023-11', name: '111'}, 
    {month: '2023-11', name: '111'}, 
    {month: '2023-11', name: '111'}, 
    {month: '2024-11', name: '111'},
    {month: '2023-10', name: '111'},
    {month: '2023-10', name: '111'},
    {month: '2023-10', name: '111'},
    {month: '2024-10', name: '111'},
    {month: '2023-09', name: '111'}, 
    {month: '2023-09', name: '111'},
    {month: '2023-09', name: '111'}, 
    {month: '2024-09', name: '111'},
    {month: '2023-08', name: '111'}, 
    {month: '2023-08', name: '111'},
    {month: '2023-08', name: '111'},
    {month: '2024-08', name: '111'},
    {month: '2023-07', name: '111'}, 
    {month: '2023-07', name: '111'},
    {month: '2023-07', name: '111'},
    {month: '2024-07', name: '111'},
    {month: '2023-06', name: '111'}, 
    {month: '2023-06', name: '111'},
    {month: '2023-06', name: '111'},
    {month: '2024-06', name: '111'},
    {month: '2023-05', name: '111'}, 
    {month: '2023-05', name: '111'}, 
    {month: '2023-05', name: '111'}, 
    {month: '2024-05', name: '111'},
    {month: '2023-04', name: '111'}, 
    {month: '2023-04', name: '111'},
    {month: '2023-04', name: '111'},
    {month: '2024-04', name: '111'},
    {month: '2023-03', name: '111'}, 
    {month: '2023-03', name: '111'}, 
    {month: '2023-03', name: '111'}, 
    {month: '2024-03', name: '111'},
    {month: '2023-02', name: '111'}, 
    {month: '2023-02', name: '111'}, 
    {month: '2023-02', name: '111'}, 
    {month: '2024-02', name: '111'},
    {month: '2023-01', name: '111'}, 
    {month: '2023-01', name: '111'}, 
    {month: '2023-01', name: '111'}, 
    {month: '2024-01', name: '111'}
]

代码实现:

// 定义数据数组(这里只展示部分,完整数据请看上方)
let data = [
    {month: '2023-12', name: '111'},
    // ... (其他数据项)
    {month: '2024-11', name: '111'}
    // ... (确保包含2024-12的数据以测试下个月的情况)
];

// 获取当前日期和时间
var now = new Date();
var currentYear = now.getFullYear();
var currentMonth = now.getMonth() + 1; // 注意月份从0开始,需要加1
var currentDay = now.getDate();
var currentHour = now.getHours();

// 定义分界点日期和时间
var cutoffDay = 26;
var cutoffHour = 8;

// 根据当前日期和时间以及分界点确定要过滤的月份
function getTargetMonth(currentYear, currentMonth, currentDay, currentHour, cutoffDay, cutoffHour) {
    if (currentDay < cutoffDay || (currentDay === cutoffDay && currentHour < cutoffHour)) {
        // 如果今天在当前月的26号8点之前
        return currentYear + '-' + String(currentMonth).padStart(2, '0');
    } else {
        // 如果今天在当前月的26号8点或之后
        var nextMonth = (currentMonth % 12 === 0) ? 1 : currentMonth + 1;
        var nextYear = (currentMonth === 12) ? currentYear + 1 : currentYear; // 更简洁的判断下一年
        return nextYear + '-' + String(nextMonth).padStart(2, '0');
    }
}

// 获取目标月份字符串
var targetMonth = getTargetMonth(currentYear, currentMonth, currentDay, currentHour, cutoffDay, cutoffHour);

// 过滤数据数组,只保留目标月份的数据
var filteredData = data.filter(function(item) {
    return item.month === targetMonth;
});

// 输出结果(在实际应用中,你可能会将这些数据用于渲染或其他逻辑)
console.log('当前日期和时间:', now.toLocaleString());
console.log('目标月份:', targetMonth);
console.log('过滤后的数据:', filteredData);

注意

        可能存在跨年份的情况(例如,从12月8点后过滤到次年的1月)。