我正在尝试使用成角度的管道,特别是以下内容:{{fechaCreacion | date: 'medium'}}
,但出现以下错误:Unable to convert Timestamp (seconds = 1528157765, nanoseconds = 878000000)" into a date 'for pipe' DatePipe'
当我刚离开{{ fechaCreacion }}
时,会显示以下内容:
我该如何解决?
我正在使用:
角度6
“ angularfire2”:“ ^ 5.0.0-rc.10”
“ firebase”:“ ^ 5.0.4”,
component.ts
aviso: any = {};
id;
titulo;
descripcion;
fechaCreacion;
categoria;
constructor( private fs: FirebaseService, private activatedRoute: ActivatedRoute) { }
ngOnInit() {
this.id = this.activatedRoute.snapshot.params['id'];
this.fs.getAvisoObject(this.id).valueChanges().forEach(aviso => {
this.titulo= aviso.titulo,
this.descripcion = aviso.descripcion,
this.fechaCreacion = aviso.fechaCreacion,
this.categoria = aviso.categoria
});
}
component.html
<mat-card class="px-3 px-md-5 py-3 py-md-4 rounded">
<div class="row no-gutters small pb-2 mb-3 d-flex justify-content-between border-bottom text-muted">
<div>
<span>{{ categoria }}</span>
</div>
<div>
<span>{{ fechaCreacion | date : 'medium' }}</span>
</div>
</div>
<h2 class="font-weight-bold">{{ titulo }}</h2>
<h5 class="font-weight-light">{{ descripcion }}</h5>
</mat-card>
service.ts
getAvisoObject(id: string) {
this.avisoObject = this.afs.doc('avisos/' + id);
return this.avisoObject;
}
答案 0 :(得分:10)
您必须调用 toDate()
,以将Firebase时间戳转换为管道之前的Javascript Date对象,例如:
{{ creationDate.toDate() | date: 'medium' }}
答案 1 :(得分:7)
在Firestore中处理时间戳类型数据(即显示的文档中的fechaCreacion
字段)时,Firestore客户端库将为您提供Timestamp类型的对象以响应查询。您需要使用此时间戳来设置日期格式,以便在浏览器中显示。
时间戳表示具有纳秒精度的时间,其中涉及两个整数,您将在屏幕上看到它们。如果要使用JavaScript Date对象,则可以在该时间戳记上使用toDate()方法进行转换,也可以使用将其呈现在屏幕上的任何方法。
答案 2 :(得分:1)
Timestamp (seconds = 1528157765, nanoseconds = 878000000)
不是有效的时间戳。如果后端使用例如toString()
答案 3 :(得分:0)
<ion-col size="4" class="top-center" item-end>
<ion-label class="second-col second-col-color text-align-right">{{ud.date.toDate() | date: 'dd/MM/yyyy'}}</ion-label>
</ion-col>
将其转换为toDate,例如: date.toDate()
答案 4 :(得分:0)
如果您想在typescript类中更改日期格式,请使用Datepipe这样
let s=new Date();
let x=this.date.transform(s,"yyyy-MM-dd")