'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.



Solution 1:[1]

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.

Solution 2:[2]

Try this in for Firebase V8.

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

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Marc Anthony B
Solution 2 Shy Penguin