如何自定义date.now格式? [英] How to customize date.now format?

查看:118
本文介绍了如何自定义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格式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆