Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to save Date as Timestamp in Angular Firestore?

firestore.Timestamp.now();

I tried this way and it saves date as map.

nanoseconds: 388000000
seconds: 1570897877

nanoseconds and seconds type are number.

I want to save date as firestore timestamp using angular. I found duplicate questions without accepted answer.

  • How to save data as Firestore Timestamp object in Angular
  • How to save date in Angular as a timestamp to Firestore?
  • How to save a date value in firestore
like image 729
Udeesha Induwara Avatar asked Oct 19 '25 09:10

Udeesha Induwara


2 Answers

If you want to add the current time as your Timestamp value in a Firestore document, you can you add it using FieldValue.serverTimestamp() as Doug mentioned, as in the following example:

import * as firebase from 'firebase';
[ . . . ]
db.collection('times').add({time: firebase.firestore.FieldValue.serverTimestamp()});

Should you want to get a specific time, you can create a new Date object and add it to your Firestore using the function toDateString() on it, as follows:

import * as firebase from 'firebase';
[ . . . ]
date = new Date('2018-11-16T09:48:51.137Z'); //sample date
this.db.collection('times').add({spec_time: this.date.toDateString()});
like image 68
Pablo Almécija Rodríguez Avatar answered Oct 21 '25 22:10

Pablo Almécija Rodríguez


import * as firebase from 'firebase';

...


const data = {
    createdAt: firebase.default.firestore.FieldValue.serverTimestamp(),
};

This code worked for me. The important part was .default from the root firebase object.

like image 38
Mikki Kobvel Avatar answered Oct 21 '25 23:10

Mikki Kobvel