How to get firestore timestamp

Paco Zevallos picture Paco Zevallos · Apr 25, 2018 · Viewed 39.9k times · Source

I'm trying to get the timestamp of a document that I created in firestore, but what I get is this:

enter image description here

myService.ts

getDomiciliarios() {
this.domiciliarios = this.afs.collection('domiciliarios').snapshotChanges().map(actions => {
  return actions.map(a => {
    const data = a.payload.doc.data() as Domiciliario;
    const id = a.payload.doc.id;
    const date = firebase.firestore.FieldValue.serverTimestamp();
    return { id, ...data, date };
  });
});

return this.domiciliarios;
}

myComponent.ts

ngOnInit() {
const domiciliarios = this.fs.getDomiciliarios()
  .subscribe(data => this.dataSource.data = data, date => date);
}

myComponent.html

<ng-container matColumnDef="fecha">
  <mat-header-cell *matHeaderCellDef mat-sort-header> Fecha </mat-header-cell>
  <mat-cell *matCellDef="let domiciliario"> {{ domiciliario.date }} </mat-cell>
</ng-container>

How can I print that timestamp, should I have previously created it?

Answer

Papa Kojo picture Papa Kojo · Jun 18, 2018

IF you want the date value of firebase.firestore.FieldValue.serverTimestamp() you can use .toDate(). See FireStore Timesteamp. In your case it will be domiciliario.date.toDate() in your template.