如何自定义 date.now 格式? [英] How to customize date.now format?
本文介绍了如何自定义 date.now 格式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
.ts
currentDate = Date.now();
.html
{{ currentDate |日期 }}
如何显示时间而不是 2019 年 10 月 25 日我想要这样:
<块引用>在 {{October}} {{2019}} 的 {{25th}} 天签署了吗?
有人已经实现了自定义时间格式吗?
解决方案
您可以为您的日期后缀实现一个自定义管道,并从角度 date
管道中获取其余部分.
import { Pipe, PipeTransform } from '@angular/core';@Pipe({ name: 'dateSuffix' })导出类 DateSuffixPipe 实现 PipeTransform {转换(日期:日期):字符串 {让天 = date.getDay();让后缀='th';如果(天 == 1 || 天 == 21 || 天 == 31){后缀 = 'st';}如果(天 == 2 || 天 == 22){后缀 = 'nd';}如果(天 == 3 || 天 == 23){后缀 = 'rd';}返回后缀;}}
HTML
签署此 {{currentDate |日期:'dd'}}{{currentDate |{{currentDate | dateSuffix}} 天日期:'LLLL yyyy'}}</div>不要忘记将您的自定义管道添加到模块声明中!
.ts
currentDate = Date.now();
.html
{{ currentDate | date }}
How can I display the time instead of Oct 25, 2019 I want it this way:
Signed this {{25th}} day of {{October}} {{2019}}?
Anyone there already implemented custom time format?
解决方案 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 格式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文