[英]How I can create Angular custom Date Pipe
I'm working on angular 5 I want to create a custom date pipe that allows me to subtract some days from a date :我正在处理 angular 5 我想创建一个自定义日期管道,允许我从日期中减去一些天数:
This how I display my date value :这是我显示日期值的方式:
<span>{{data.nextCertificateUpdate | date:'yyyy-MM-dd'}}</span>
for example this display a value like : 2018-08-29例如,这显示一个值,如: 2018-08-29
I ask if it's possible to create a pipe that allow me to substract a number of days for example 28 from this date ?我问是否可以创建一个管道,允许我从这个日期减去天数,例如 28 天?
Something like :就像是 :
<span>{{data.nextCertificateUpdate | mypipe:28 }}</span>
and this should display value like : 2018-08-01这应该显示如下值: 2018-08-01
Thanks for any help谢谢你的帮助
Adding to the nice answer given by Sachila above, you can also implement the full functionality in your custom pipe itself.除了上面 Sachila 给出的不错的答案之外,您还可以在自定义管道本身中实现全部功能。
import { Pipe, PipeTransform } from '@angular/core';
import { DatePipe } from '@angular/common';
@Pipe({ name: 'mypipe' })
export class Mypipe implements PipeTransform {
// adding a default format in case you don't want to pass the format
// then 'yyyy-MM-dd' will be used
transform(date: Date | string, day: number, format: string = 'yyyy-MM-dd'): string {
date = new Date(date); // if orginal type was a string
date.setDate(date.getDate()-day);
return new DatePipe('en-US').transform(date, format);
}
}
And use your custom Pipe like:并使用您的自定义管道,例如:
<span>{{data.nextCertificateUpdate | mypipe: 28: 'yyyy-MM-dd'}}</span>
See a working example here: https://stackblitz.com/edit/angular-995mgb?file=src%2Fapp%2Fapp.component.html在此处查看工作示例: https : //stackblitz.com/edit/angular-995mgb?file=src%2Fapp%2Fapp.component.html
You can create class for property like wise I have use environment class for date format DATE_FORMAT and assign by default dd-MM-yyyy format and use in date pipe.您可以为属性创建类,就像我已将环境类用于日期格式 DATE_FORMAT 并默认分配 dd-MM-yyyy 格式并在日期管道中使用。 By this approach only change the value of DATE_FORMAT and we can easily change the format of date else where.
通过这种方法只更改 DATE_FORMAT 的值,我们可以轻松地更改其他地方的日期格式。
import { Pipe, PipeTransform } from '@angular/core';
import { environment } from "../../../../environments/environment";
import { DatePipe } from "@angular/common";
@Pipe({
name: 'dateFormat'
})
export class DateFormatPipe extends DatePipe implements PipeTransform {
transform(value: any, args?: any): any {
if(Object.keys(environment).indexOf("DATE_FORMAT") >= 0){
return super.transform(value, environment.DATE_FORMAT);
}
return super.transform(value, 'dd-MM-yyyy');
}
html html
<span>{{ data.date | dateFormat }}</span>
Create a custom pipe call mypipe
创建自定义管道调用
mypipe
import { Pipe, PipeTransform } from '@angular/core';
@Pipe({ name: 'mypipe' })
export class Mypipe implements PipeTransform {
transform(date: Date, day: number): string {
date.setDate(d.getDate()-day);
return date;
}
}
call it like this像这样称呼它
<span>{{data.nextCertificateUpdate | mypipe:28 | date:'yyyy-MM-dd'}}</span>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.