1). generate
会创建pipetime-interval.ts
,并添加到PipsModule
中。
...
import { TimeIntervalPipe } from './time-interval';
@NgModule({
declarations: [TimeAgoPipe, TimeIntervalPipe],
imports: [],
exports: [TimeAgoPipe, TimeIntervalPipe]
})
...
2). 修改pip
import { Pipe, PipeTransform } from '@angular/core';
import { DatePipe } from '@angular/common';
@Pipe({
name: 'timeInterval',
})
export class TimeIntervalPipe implements PipeTransform {
transform(date: any, args?: any) {
if (date){
let interval = date as number;
let newDate = new Date();
newDate.setTime(interval * 1000);
let datePipe = new DatePipe("en-US");
return datePipe.transform(newDate, 'yyyy/MM/dd');
}
}
}
3). 使用
需要使用的模块导入
...
import { PipesModule } from '../../pipes/pipes.module';
@NgModule({
declarations: [
IndustrialPolicyPage,
],
imports: [
IonicPageModule.forChild(IndustrialPolicyPage),
PipesModule
],
})
...
<p>{{ item.ts | timeInterval }}</p>
ps: html中使用时注意大小写