DEV
DEV

Reputation: 495

How to convert firebase time stamp to Millisecond?

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.

Upvotes: 1

Views: 1587

Answers (2)

Fiston Emmanuel
Fiston Emmanuel

Reputation: 4849

Try this in for Firebase V8.

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

Upvotes: 0

Marc Anthony B
Marc Anthony B

Reputation: 4069

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.

Upvotes: 2

Related Questions