gpt4 book ai didi

javascript - Angular 中的日期序数(st,nd,rd,th)?

转载 作者:行者123 更新时间:2023-11-29 20:33:40 29 4
gpt4 key购买 nike

我需要低于日期格式。

30th July 2019

我的尝试,

<time-zone  time="{{ 2019-07-31 18:30:00 }}" format="DD MMM YYYY"></time-zone>

结果:2019 年 8 月 1 日

最佳答案

您可以创建自定义序号日期管道,例如

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

@Pipe({
name: 'ordinalDate'
})
export class OrdinalDatePipe implements PipeTransform {
transform(value: Date): string {
if (!value) {
return '';
}
let months= ["January","February","March","April","May","June","July",
"August","September","October","November","December"]
return `${value.getDate()}${this.nth(value.getDate())} ${months[value.getMonth()]} ${value.getFullYear()}`;
}

nth(d) {
if (d > 3 && d < 21) return 'th';
switch (d % 10) {
case 1: return "st";
case 2: return "nd";
case 3: return "rd";
default: return "th";
}
}
}

将管道添加到模块声明,然后在你的日期使用它

{{dateToFormat | ordinalDate}}

StackBlitz Example

Logic inspired by this SO

关于javascript - Angular 中的日期序数(st,nd,rd,th)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57351852/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com