如何自定义 date.now 格式?

编程入门 行业动态 更新时间:2024-10-27 09:36:29
本文介绍了如何自定义 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 y​​yyy'}}</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 格式?

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

发布评论

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

>www.elefans.com

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