本文介绍了如何自定义date.now格式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
.ts
currentDate = Date.now();.html
{{ currentDate | date }}我该如何显示时间而不是2019年10月25日:
How can I display the time instead of Oct 25, 2019 I want it this way:
在{{October}} {{2019}}的{{25th}}天签名了吗?
Signed this {{25th}} day of {{October}} {{2019}}?
有人在实施自定义时间格式吗?
Anyone there already implemented custom time format?
推荐答案您可以为日期后缀实现自定义管道,并从有角的date管道中获取其余管道.
You can implement a custom pipe for your date suffix and take the rest from the angular date pipe.
import { Pipe, PipeTransform } from '@angular/core'; @Pipe({ name: 'dateSuffix' }) export class DateSuffixPipe implements PipeTransform { transform(date: Date): string { let day = date.getDay(); let suffix = 'th'; if (day == 1 || day == 21 || day == 31) { suffix = 'st'; } if (day == 2 || day == 22) { suffix = 'nd'; } if (day == 3 || day == 23) { suffix = 'rd'; } return suffix; } }HTML
<div> Signed this {{currentDate | date:'dd'}}{{currentDate | dateSuffix}} day of {{currentDate | date:'LLLL yyyy'}} </div>别忘了将自定义管道添加到模块声明中!
Don't forget to add your custom pipe to module declarations!
更多推荐
如何自定义date.now格式?
发布评论