Angular:将UTC转换为本地日期时间

问题描述 投票:0回答:3

我想转换2018-04-03T06:08:08-05:00

MM.dd.yyyy HH:mm

我如何在Angular 4.x +中执行此操作?

angular
3个回答
1
投票

与使用日期使用getHours(),getMinutes()等的vanilla javascript相同。以下是对可能有用的类似问题的引用。

Current time formatting with Javascript


1
投票

尝试使用:

在html中:

<p>Formatted  Date: <b>{{ newdate | date:'MM.dd.yyyy HH:mm' }}</b> </p>

在组件中:

newdate = new Date('2018-04-03T06:08:08-05:00');

输出:

Formatted Date: 04.03.2018 22:29

Stackblitz


0
投票

你需要检查一下:https://angular.io/api/common/DatePipe

正如它在链接中所说,您可以在组件中声明日期,例如:

export class DatePipeComponent {
  today = Date.now();
}

然后使用管道将其转换为所需的格式:

{{today | date:'yyyy-MM-dd HH:mm a z':'+0900'}}
© www.soinside.com 2019 - 2024. All rights reserved.