按日期过滤列表数据 angular 4
Filter list data by dates in angular 4
我有一个包含用户生日的列表。我想显示从现在开始生日为 1 的用户列表。
我想显示从今天起一周内生日的用户列表。
我的清单就像
`<nb-list-item *ngFor="let yuvak of yuvaklist | slice:startDate:endDate ">
{{yuvak.dateofbirth}}
{{yuvak.firstname}} + + {{yuvak.lastname}}
</nb-list-item>`
下面的简单示例:
let weekSec = 7 * 24 * 60 * 60 * 1000;
let today = new Date();
let birthday = new Date('1995-11-22');
birthday.setFullYear(today.getFullYear());
if (birthday.getTime() < today.getTime() && birthday.getTime() + weekSec > today.getTime()) {
console.log('Happy birthday');
} else {
console.log('...');
}
同意@incNick 代码,但我们应该将其包装在 Pipe 中。您可以像这样创建 Angular 管道过滤器。
从“@angular/core”导入{管道、管道转换};
@Pipe({
name: 'birthdayFilter',
pure: false
})
export class FilterPipe implements PipeTransform {
transform(items: any[], args: any[]): any {
return items.filter(item => this.isBirthdayInaWeek(item.birthday));
}
isBirthdayInaWeek(birthday){
let weekSec = 7 * 24 * 60 * 60 * 1000;
let today = new Date();
let birthday = new Date(birthday);
birthday.setFullYear(today.getFullYear());
if (birthday.getTime() < today.getTime() && birthday.getTime() + weekSec > today.getTime()) {
return true;
} else {
return false;
}
}
}
模板:
<nb-list-item *ngFor="let yuvak of yuvaklist | birthdayFilter ">
{{yuvak.dateofbirth}}
{{yuvak.firstname}} + + {{yuvak.lastname}}
</nb-list-item>
我有一个包含用户生日的列表。我想显示从现在开始生日为 1 的用户列表。
我想显示从今天起一周内生日的用户列表。
我的清单就像
`<nb-list-item *ngFor="let yuvak of yuvaklist | slice:startDate:endDate ">
{{yuvak.dateofbirth}}
{{yuvak.firstname}} + + {{yuvak.lastname}}
</nb-list-item>`
下面的简单示例:
let weekSec = 7 * 24 * 60 * 60 * 1000;
let today = new Date();
let birthday = new Date('1995-11-22');
birthday.setFullYear(today.getFullYear());
if (birthday.getTime() < today.getTime() && birthday.getTime() + weekSec > today.getTime()) {
console.log('Happy birthday');
} else {
console.log('...');
}
同意@incNick 代码,但我们应该将其包装在 Pipe 中。您可以像这样创建 Angular 管道过滤器。
从“@angular/core”导入{管道、管道转换};
@Pipe({
name: 'birthdayFilter',
pure: false
})
export class FilterPipe implements PipeTransform {
transform(items: any[], args: any[]): any {
return items.filter(item => this.isBirthdayInaWeek(item.birthday));
}
isBirthdayInaWeek(birthday){
let weekSec = 7 * 24 * 60 * 60 * 1000;
let today = new Date();
let birthday = new Date(birthday);
birthday.setFullYear(today.getFullYear());
if (birthday.getTime() < today.getTime() && birthday.getTime() + weekSec > today.getTime()) {
return true;
} else {
return false;
}
}
}
模板:
<nb-list-item *ngFor="let yuvak of yuvaklist | birthdayFilter ">
{{yuvak.dateofbirth}}
{{yuvak.firstname}} + + {{yuvak.lastname}}
</nb-list-item>