我希望能够显示我的数组并能够按日期进行过滤。 例如介于“ 01-02-2017”和“ 02-02-2017”之间。
我尝试这样做,但我认为即时通讯实施不正确
export class AppComponent {
snapshots = [
{ date: '01-02-2017', rooms: 2869, users: 1783, location: 'All' },
{ date: '01-05-2017', rooms: 2769, users: 1655, location: 'All' },
{ date: '03-02-2017', rooms: 2025, users: 1911, location: 'All' },
{ date: '01-02-2017', rooms: 1278, users: 1167, location: 'All' },
{ date: '02-02-2017', rooms: 2028, users: 1940, location: 'All' },
{ date: '01-10-2017', rooms: 2113, users: 2001, location: 'All' },
{ date: '03-02-2017', rooms: 2654, users: 1841, location: 'All' },
{ date: '01-02-2017', rooms: 1264, users: 1140, location: 'All' },
{ date: '01-02-2017', rooms: 2918, users: 2557, location: 'All' },
{ date: '01-20-2017', rooms: 2160, users: 2112, location: 'All' }
];
start;
end;
ngOnInit() {
}
filter(){
this.snapshots = this.snapshots.filter(m => {
if ( m.date > this.start && m.date < this.end)
return this.snapshots
})
}
}
答案 0 :(得分:1)
您需要使用getTime()
类的Date
函数
filter(){
this.snapshots = this.snapshots.filter(m => (m.date.getTime() > this.start.getTime()) && (m.date.getTime() < this.end.getTime()))
}
答案 1 :(得分:0)
最安全的方法将需要将它们全部转换为JavaScript Date对象。您不能只在JavaScript字符串上使用getTime()。另外,您将必须返回“ m”而不是“ this.snapshots”。
filter(){
this.snapshots = this.snapshots.filter(m => {
if (new Date(m.date).getTime() > new Date(this.start).getTime() && new Date(m.date).getTime() < new Date(this.end).getTime()) {
return m;
}
})
}