问题产生的原因是由于 Angular 时间管道显示本地时间而不是 UTC。为了解决这个问题,我们可以使用 DatePipe 中的 transform 方法并指定要显示的时区。
例如,如果我们想显示 UTC 时间,则可以按如下方式实现:
import { DatePipe } from '@angular/common';
constructor(private datePipe: DatePipe) {}
const now = new Date(); const utcNow = this.datePipe.transform(now, 'yyyy-MM-dd HH:mm:ss', 'UTC'); console.log('UTC time:', utcNow);
这样就可以正确地显示 UTC 时间了。
上一篇:Angular事件发射问题