Angular 2/4/5 在表达式中显示带有后缀(st、rd 和 th)的日期

标签 angular

在 Angular 2/4/5/... 中向日期添加后缀(st、rd 和 th)的最佳“Angular 方式”是什么?

例如:Angular 表达式中的 2 月 25 日、3 月 1 日、4 月 2 日 {{item |日期:???}}

后缀选项似乎没有包含在 Angular Date Pipe 中医生。

最佳答案

谢谢罗迪,

与此同时,Angular 团队为日期后缀提出了一些核心功能,这是我提出的简单管道。

import { Pipe, PipeTransform } from '@angular/core';

@Pipe({ name: 'dateSuffix' })
export class DateSuffix implements PipeTransform {
    transform(value: string): string {

    let suffix = 'th',
        day = value;

        if (day === '1' || day === '21' || day === '31') {
            suffix = 'st'
        } else if (day === '2' || day === '22') {
            suffix = 'nd';
        } else if (day === '3' || day === '23') {
           suffix = 'rd';
        }

        return suffix;

    }
}

一旦导入到组件中...

我用这样的表达方式

{{record.RecordDate | date: "MMM.d"}} 
<span class="text-superscript">
    {{(record.RecordDate | date: "d") | dateSuffix}}
</span>

输出类似 Apr.3rd 的内容

关于Angular 2/4/5 在表达式中显示带有后缀(st、rd 和 th)的日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48628263/

相关文章:

javascript - Angular-cli 更新应用程序设置,例如 '' 前缀”

angular - npm install 没有一个好的错误就被杀死了

angular - 调用了如何解决 "Function calls are not supported in decorators but ' StoreModule'。”

javascript - karma 与 Jasmine 共存?

Angular 组合与继承 - 使用 @Inputs

Angular ng 内容 : how to find element inside?

angular - 当父组件上的变量更改时重新加载子组件。 Angular 2

javascript - 在 Angular 2 + Meteor 应用程序中包含 Google 字体

html - 添加 Angular 4 时的事件元素

angular - 如何在页面刷新时重置路由器?