How to get firestore timestamp

2020-06-13 20:06发布

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?

4条回答
在下西门庆
2楼-- · 2020-06-13 20:24

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.

查看更多
我只想做你的唯一
3楼-- · 2020-06-13 20:31

Thanks to @Renaud who put emphasis on where I was implementing the timestamp, it should be when creating a record in Firestore. In my case I am working with a formGroup, then the code would look something like this:

forma: FormGroup;

constructor( fb: FormBuilder) { 
  this.forma = fb.group ({
    field1: [ ''],
    field2: [ ''],
    ...
    myDate: [ firebase.firestore.FieldValue.serverTimestamp() ],
  });
}

This alternative also works but remember that it is the current time of the client's computer (user's browser).

forma: FormGroup;

constructor( fb: FormBuilder) { 
  this.forma = fb.group ({
    field1: [ ''],
    field2: [ ''],
    ...
    myDate: [ new Date() ],
  });
}
查看更多
Juvenile、少年°
4楼-- · 2020-06-13 20:39

I think you are using FieldValue.serverTimestamp() the wrong way: as the documentation states, firebase.firestore.FieldValue methods return "values that can be used when writing document fields with set() or update()". (See https://firebase.google.com/docs/reference/js/firebase.firestore.FieldValue)

You are using the serverTimestamp() method while reading the data.

You should use it when you create the records in the database, as you mention at the end of your question.

EDIT: Do as follow:

const timestamp = firebase.firestore.FieldValue.serverTimestamp();
docRef.update({ updatedAt: timestamp });

You can then query like

collectionRef.orderBy('updatedAt').get()
    .then(snapshot => {
        snapshot.forEach(doc => {
            ...
        });
    })
    .catch(err => {
        console.log('Error getting documents', err);
    });

The code above is pure JavaScript and you may adapt it to angular and type script but the philosophy is the same.

查看更多
趁早两清
5楼-- · 2020-06-13 20:41

If you want the current Date as a timestamp you can use the following

For Firebase Functions

import admin = require('firebase-admin');

const todayAsTimestamp = admin.firestore.Timestamp.now()

For local projects

import { Timestamp } from '@google-cloud/firestore';

const myTimestampAsDate = Timestamp.now()
查看更多
登录 后发表回答