JS Dates and Callable Functions
ISO strings are the better choice
When interacting with Callable Functions, it's best to represent dates as ISO strings. It is simple to reason about: the value and the type stay consistent on the client and on the server.
If we were to work with Date fields or Firestore Timestamps fields, the values are not consistent when sending to the server and when receiving from the server.
In this article, we explain what happens when we send Date and Timestamp objects to Callable Functions or when we receive them from Callable Functions. Before being sent, both are serialized to JSON.
sending to Callable Functions
Timestamp is a Firestore specific type and doesn't get a special treatment: it serializes to an object with seconds and nanoseconds (through toJSON()).
timestamp: { seconds: 1696751687, nanoseconds: 527000000 },
As for fields of type Date, they serialize to an ISO string (through toJSON()):
date: "2023-10-08T07:54:47.527Z"
We could technically instantiate a Timestamp or a Date:
new Timestamp(timestamp.seconds, timestamp.nanoseconds)
new Date(date)
sending from Callable functions
If we attempt to return a Date object, it serializes to an ISO string.
If we attempt to return a Timestamp object, it serializes to the internal representation, possible an object with _seconds and _nanoseconds. We should avoid this pattern.