【问题标题】:Filter data by minutes VueJs按分钟过滤数据 VueJs
【发布时间】:2020-01-28 19:33:05
【问题描述】:

我的 VueJs 页面中有一个数据表,并通过 ajax 请求获取其数据。返回数据是这样的

{
    name: "Test Name",
    created_at: "2019-06-14 07:31:15"
}

我的按钮应该使用以下规则过滤数据:

Created data within the last 5 mins
Created data within the last 15 mins
Created data within the last 30 mins

鉴于我的created_at,我该怎么做?我有这个当前功能

computed: {
    filterData() {
        return this.customers.filter(item => item.created_at.includes(new Date().getDay()+1));
    }
}

数据this.customers是我的ajax请求数据。

我只是从这里引用它 filter response data date wise vue 。基于此,我希望数据被 1 天前的数据过滤。但这对我也不起作用。也许是因为我的日期是日期时间而不是日期?

【问题讨论】:

标签: javascript datetime vue.js time filter


【解决方案1】:

如果你的日期格式是默认JS Date函数可解析的,你可以试试下面的代码。两个日期之间的减号 (-) 将以毫秒为单位给出差异。除以 60 * 1000 以分钟为单位得出差异。

a = [{
    name: "Test Name",
    created_at: "2019-09-29 10:00:15"
},
{
    name: "Test Name",
    created_at: "2019-09-29 10:21:15"
},
{
    name: "Test Name",
    created_at: "2019-09-29 09:21:15"
},
{
    name: "Test Name",
    created_at: "2019-09-29 09:50:15"
}]

const filterByLastMins = (mins, arr) => {
    const currentTime = new Date();
    return arr.filter((elem) => {
        const diffInMins = (currentTime -  Date.parse(elem.created_at)) / (1000 * 60);
        return diffInMins < mins
    })
}

res = filterByLastMins(60, a);
console.log(res);

【讨论】:

  • 感谢非常适合我。我不想使用另一个库来计算分钟数。
猜你喜欢
  • 2019-10-24
  • 2021-07-02
  • 2016-06-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-11-18
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多