How to get firestore timestamp

后端 未结 4 2251
孤城傲影
孤城傲影 2021-02-07 19:18

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

myService.ts

getDomiciliari         


        
4条回答
  •  自闭症患者
    2021-02-07 19:47

    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() ],
      });
    }
    

提交回复
热议问题