如何自定义date.now格式?

编程入门 行业动态 更新时间:2024-10-27 07:23:26
本文介绍了如何自定义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格式?

本文发布于:2023-10-24 02:55:39,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1522735.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:自定义   格式   date

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!