您可以通过添加新的Custom Pipe 或在您的组件中处理您的DatePipe (Angular built-in)
已附上 Stackblitz Demo 供您参考
方法 #1 - 自定义管道
@Pipe({
name: 'dateAtInsert'
})
export class DateAtInsertPipe implements PipeTransform {
transform(value: string) {
return value
.replace(/,\s(?=\d+:)/g, ' ')
.split(/\s(?=\d+:)/g)
.join(' at ');
}
}
{{ today | date : dateFormat | datAtInsert }} // Dec 8, 2020 at 8:26 AM
方法 #2 - 组件中的日期管道
const today = new Date();
const transformDate = this.datePipe.transform(today, this.dateFormat);
this.formattedDate = transformDate
.replace(/,\s(?=\d+:)/g, ' ')
.split(/\s(?=\d+:)/g)
.join(' at ');
<h1>{{ formattedDate }}<h1> // Dec 8, 2020 at 8:26 AM
方法#3 - 在日期格式中添加at(组件中的DatePipe)
dateFormat: string = 'MMM d, y AT h:mm a'; // Need to use an uppercase AT since lowercase "a" is reserved with AM/PM in date formats
// While capital "A" and "T" doesn't hold any value in date formats so it's safe to use
const transformDate = this.datePipe.transform(this.today, this.dateFormat);
this.formattedDate = transformDate.replace('AT', 'at');
方法 #4 - 在日期格式中添加 at(HTML 中的 DatePipe)
模板
{{ insertDateAt(today | date: 'MMM d, y AT h:mm a') }}
组件
insertDateAt(date: string): string {
return date.replace('AT', 'at');
}
注意: