Home > Software design >  How to convert firebase time stamp to Millisecond?
How to convert firebase time stamp to Millisecond?

Time:03-17

I am using Cloud firestore to store data. I want to convert timestamp field data to Millisecond.

Data I am getting in below format

{"nanoseconds": 627000000, "seconds": 1647432933}

I want to convert it to Millisecond.

CodePudding user response:

Firestore have a method called toMillis(). You can use it like this. See code below:

    const docSnap = await getDoc(docRef);

    if (docSnap.exists()) {
      console.log("timestamp:", docSnap.data().timestamp.toMillis());
    } else {
      // doc.data() will be undefined in this case
      console.log("No such document!");
    }

will return something like this:

timestamp: 1647438836443

For more information, you may refer to this documentation.

CodePudding user response:

Try this in for Firebase V8.

 const getServerTime = () =>
firebase.firestore?.Timestamp?.now().toMillis();

  • Related