Firebase Firestore recently changed how they manage timestamps, and I'm unable to retrieve a date
object from the timestamp.
How do I get a date object from the new firebase.firestore.FieldValue.serverTimestamp()
?
To convert a Firestore date or timestamp to a JavaScript Date, we use firebase. firestore. Timestamp. fromDate to convert the a date to a Firestore timestamp.
elements : any [] ) : FieldValue. Returns a special value that can be used with set() or update() that tells the server to union the given elements with any array value that already exists on the server. Each specified element that doesn't already exist in the array will be added to the end.
firestore. Timestamp. A Timestamp represents a point in time independent of any time zone or calendar, represented as seconds and fractions of seconds at nanosecond resolution in UTC Epoch time. It is encoded using the Proleptic Gregorian Calendar which extends the Gregorian calendar backwards to year one.
You just need to do this:
yourFirestoreTimestamp.toDate()
If you look at the Timestamp
object in the console you'll see that .toDate()
is a function available by default on each Timestamp
.
--
However, please note that when you haven't synced with Firestore yet
FieldValue.serverTimestamp()
gives nothing of value.
Only after you have synced with Firestore and the data is fetched again, the prop will be changed from serverTimestamp
→ to Timestamp
You'll also see much more information saved in a Timestamp
object:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With