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

时间:2018-04-03 16:14:58

标签: angular

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

MM.dd.yyyy HH:mm

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

4 个答案:

答案 0 :(得分:1)

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

Current time formatting with Javascript

答案 1 :(得分: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

答案 2 :(得分:0)

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

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

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

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

{{today | date:'yyyy-MM-dd HH:mm a z':'+0900'}}

答案 3 :(得分:0)

所有这些都是您可以在角度中使用时区的多种方式:-

{{currentDate | date:'short':'UTC +9:30 / +10:30'}}
{{currentDate | date:'short':'GMT'}}
{{currentDate | date:'short':'-0600'}}
{{currentDate | date:'short':'UTC+4'}}

日期格式如下:-

medium: Sep 9, 2019, 12:35:54 PM
long: September 9, 2019 at 12:35:54 PM GMT+5
full: Tuesday, September 9, 2019 at 12:35:54 PM GMT+05:30
shortDate: 09/3/19
mediumDate: Sep 9, 2019
longDate: September 9, 2019
fullDate: Tuesday, September 9, 2019
shortTime: 12:35 PM
mediumTime: 12:35:54 PM
longTime: 12:35:54 PM GMT+5
fullTime: 12:35:54 PM GMT+05:30

所有这些都是不同的时区示例:-

Alpha Time Zone 'UTC+1'
Arabia Standard Time 'UTC+3'
Central Standard Time 'UTC-6'
China Standard Time 'UTC+8'
Delta Time Zone 'UTC+4'
Greenwich Mean Time 'UTC+0'
Gulf Standard Time 'UTC+4'
Hawaii Standard Time 'UTC-10'
India Standard Time 'UTC+4'