Angular 日期管道角度4带时区的自定义格式(短)

Angular 日期管道角度4带时区的自定义格式(短),angular,datetime,date-pipe,Angular,Datetime,Date Pipe,我需要打印带有时区的时间戳自定义格式(短格式): 输出: 2017年6月29日晚上7:25 但我需要附加时区,例如: import { Pipe } from "@angular/core"; import { DatePipe } from "@angular/common"; @Pipe({ name: "myDate", pure: true }) export class MyDatePipe extends DatePipe { transform(value

我需要打印带有时区的时间戳自定义格式(短格式):

输出:

2017年6月29日晚上7:25

但我需要附加时区,例如:

import { Pipe } from "@angular/core";
import { DatePipe } from "@angular/common";

@Pipe({
    name: "myDate",
    pure: true
})
export class MyDatePipe extends DatePipe {
    transform(value: any, pattern: string = "mediumDate"): string|null {
        let result = super.transform(value, pattern);
        result += " " + this.map[Intl.DateTimeFormat().resolvedOptions().timeZone];
        return result;
    }
    map = {
        "Asia/Calcutta": "IST"
    };
}
2017年6月29日晚上7:25IST

Angular为时区选项提供了
'z'
'z'
,但没有一个选项给出预期的结果:

'MMM dd, y hh:mm a  z' ==> Jun 29, 2017 07:25 PM India Standard Time
'MMM dd, y hh:mm a  Z' ==> Jun 29, 2017 07:25 PM GMT+5:30

我想要
2017年7月7日12:27:01 AM IST

日期管道使用Intl格式化日期。因此,由站点所在的系统决定以何种格式返回当前时区。为了实现所需的行为,我建议您创建一个自定义DatePipe,它将返回所需的timezoe缩写。例如:

import { Pipe } from "@angular/core";
import { DatePipe } from "@angular/common";

@Pipe({
    name: "myDate",
    pure: true
})
export class MyDatePipe extends DatePipe {
    transform(value: any, pattern: string = "mediumDate"): string|null {
        let result = super.transform(value, pattern);
        result += " " + this.map[Intl.DateTimeFormat().resolvedOptions().timeZone];
        return result;
    }
    map = {
        "Asia/Calcutta": "IST"
    };
}

但是,您需要将每个可能的时区添加到
map
对象中。请参阅说明此方法的说明。

您能否详细说明“未给出预期结果”?是显示不正确的时区还是根本不显示?添加
Z
似乎可以提供正确的时区。它以3个字符的格式与我的时区完全匹配。请查收。谢谢。我想你需要编写自己的日期格式化管道来处理这个问题。它可以使用日期/时间格式的现有管道返回一个值,然后操纵时区以返回3位版本而不是全文。是的,这会有所帮助。非常感谢。让我们看看angular team是否在即将发布的版本中使用较短的时区格式进行更新。。!!我认为这甚至不会实现,因为这既不取决于角度,也不取决于系统设置。它们可能因浏览器和操作系统的不同而有所不同。所以,你可能会有不同的结果。有没有办法使用夏令时