Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is there a difference between servertimestamp and the js new Date() in Firestore?

While writing a doc to the Firestore, I often see that the difference between the serverTimestamp() sentinel and the new Date() object is non-zero.

The difference ranges between a few seconds to tens of minutes.

Aren't they doing the same thing?

enter image description here

Code for the example in Cloud Functions

docRef.set({
  'date-timestamp': new Date(),
  'server-timestamp': admin.firestore.FieldValue.serverTimestamp(),
}).catch(console.error);
like image 440
Utkarsh Bhatt Avatar asked Oct 28 '25 08:10

Utkarsh Bhatt


1 Answers

They are not doing the same thing.

serverTimestamp() converts to the date as reckoned by Google servers, at the time the document write was received at the server.

new Date() yields the date as reckoned by the client at the time the Date object was created, regardless of any other factors.

Obviously, in your example, the reckoning of time is different between the client and the server. There will always be some difference, as there is latency between the client and the server that can't be avoided.

like image 58
Doug Stevenson Avatar answered Oct 29 '25 23:10

Doug Stevenson