Angular datepipe将日期转换为GMT格式

huangapple go评论82阅读模式
英文:

Angular datepipe convert date to GMT format

问题

在我的Angular应用程序中,我试图使用Datepipe将日期转换为GMT格式并显示。
我在一个博客中看到Datepipe使用系统区域设置来转换日期。
如何转换成GMT。

假设日期是6/19/2023 12:00:00 AM,我想显示XXXXX(GMT-5)。
我尝试了以下代码片段,但不起作用。

new Date(datePipe.transform(new Date(6/19/2023 12:00:00 AM, 'MM/DD/YY')))
英文:

In my Angular application I am trying to use Datepipe to convert date into GMT format and display.
I see in a blog that Datepipe uses systems locale to convert the date.
How can I convert into GMT.

Assuming the date is 6/19/2023 12:00:00 AM, I want to dislay XXXXX (gmt-5)
I tried following code snippet which is not working.

new Date(datePipe.transform(new Date(6/19/2023 12:00:00 AM, 'MM/DD/YY'))

答案1

得分: 1

为了将时间转换为GMT-5,您可以使用以下代码:

constructor(private datePipe: DatePipe) {
    const date = new Date('6/19/2023 12:00:00 AM');
    this.formattedDate = this.datePipe.transform(date, 'short', '-0500');
    console.log(this.formattedDate); // 显示格式化后的日期
}

演示代码:https://stackblitz.com/edit/angular-ivy-hnkmjs

英文:

For converting to GMT-5, you can use transform as this code

constructor(private datePipe: DatePipe) {
    const date = new Date('6/19/2023 12:00:00 AM');
    this.formattedDate = this.datePipe.transform(date, 'short', '-0500');
    console.log(this.formattedDate); // Display the formatted date
}

Demo code: https://stackblitz.com/edit/angular-ivy-hnkmjs

huangapple
  • 本文由 发表于 2023年6月22日 11:03:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/76528358.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定