If you are using FieldValue.serverTimestamp()
it means you let Firestore generate a timestamp for you. Please note that this timestamp is generated entirely on the server. It is not generated as document ids are, for instance on the client.
I cannot find a way to calculate anything using it since it's return type is FieldValue and I am unable to parse it to Timestamp.
When calling FieldValue's serverTimestamp() method, it true that type of the object that is returned is FieldValue
but this only used to be passed as an argument when you need that timestamp property. Please check how you can add a timestamp to a Cloud Firestore document:
So if you query the database and you get a document that has a property that was set using FieldValue.serverTimestamp()
, the type of object that is returned is Date. Since you get a Date
object back, you can do whatever you want with it.
However, in Firebase Realtime Database we have another mechanism. So please see this answer:
So, in this case, it's totally different since we are using the time since the Unix epoch, in milliseconds and not a Date
object.
Edit:
It sounds like your question started off about how to get a Timestamp from Firestore, but is actually just about how to structure a document so two devices can read the same document. However, to make it work, you should use two properties of type Date
. One for device A and one for device B. Check the schema below:
Firestore-root
|
--- yourCollection (collection)
|
--- yourDocument (document)
|
--- deviceA: January 24, 2020 at 3:37:59 PM UTC+3
|
--- deviceB: January 25, 2020 at 3:37:59 PM UTC+3 //Timestamp
To get the values of deviceA
and deviceB
properties, please use my answer from the following post:
Now you can compare both timestamps.