In Firestore, you can store datetimes in their proprietary "date" format or as the number of milliseconds since 1970. The first one requires you to specifically call the toDate()
method to turn it into a Date, but the second one is already widely accepted as a way of storing dates. Is there any advantage of using the first method as both methods provide the same functionality?
CodePudding user response:
I assume you're referring to the Firestore ServerTimestamp
. There is a great article written about this topic by Doug here
To sum it up: The Firestore Timestamp gets calculated server side, ensuring that it captures the exact moment in time the server received the request. Since it's calculated on the server, it's also easy to write security rules without the worry of the end user manipulating the Timestamp they submit in their request.
CodePudding user response:
Firestore's Timestamp
type has microsecond (instead of millisecond) accuracy and (as Huydra also said in their excellent answer) can be set automatically to the current value by the server.
Typically the microsecond vs millisecond precision is not a big deal, but the ability to have the server set/validate the current time means you no longer have to depend on the client to send an accurate time (which in reality: they won't).