=>这是我的json数据。
newUser = [
{"id":1,"name":"viky","birthDate":"18/06/2019"},
{"id":2,"name":"chetan","birthDate":"3/07/2019"},
{"id":3,"name":"khushi","birthDate":"11/12/2019"},
{"id":4,"name":"nency","birthDate":"19/04/2019"},
{"id":5,"name":"urmil","birthDate":"27/03/2019"},
];
当我单击(过去7天)选项,然后向用户显示过去7天哪个生日。
答案 0 :(得分:1)
您可以使用矩量库的diff方法
newUser = [
{ "id": 1, "name": "viky", "birthDate": "18/06/2019" },
{ "id": 2, "name": "chetan", "birthDate": "3/07/2019" },
{ "id": 3, "name": "khushi", "birthDate": "11/12/2019" },
{ "id": 4, "name": "nency", "birthDate": "19/04/2019" },
{ "id": 5, "name": "urmil", "birthDate": "27/03/2019" },
];
constructor() {
console.log(this.newUser.filter(c => Math.abs((moment().diff(moment(c.birthDate, 'DD/MM/YYYY'), 'days'))) <= 7));
}
答案 1 :(得分:1)
如果您不想安装和管理外部依赖项,这里是Vanilla JavaScript方法。
我们将今天的日期以及数组中的birthDate
都转换为毫秒。然后,我们过滤newUser
数组,并检查单个日期是否在过去7天内。
const newUser = [
{id:1, name: 'viky', birthDate: '18/06/2019'},
{id:2, name: 'chetan', birthDate: '3/07/2019'},
{id:3, name: 'khushi', birthDate: '11/12/2019'},
{id:4, name: 'nency', birthDate: '19/04/2019'},
{id:5, name: 'urmil', birthDate: '27/03/2019'},
{id:6, name: 'urmil2', birthDate:'19/06/2019'},
{id:7, name: 'urmil3', birthDate:'15/06/2019'},
];
const today = new Date().setHours(0,0,0,0);
const res = newUser.filter(user => {
dateSplit = user.birthDate.split('/');
const birthDate = new Date(dateSplit[2], dateSplit[1] - 1, dateSplit[0]).getTime();
const difference = (today - birthDate) / (1000*60*60*24);
return difference <= 7 && difference >= 0;
});
console.log(res);