Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get a JavaScript Date object from the new `firebase.firestore.FieldValue.serverTimestamp()`

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()?

like image 502
mesqueeb Avatar asked Jul 19 '18 06:07

mesqueeb


People also ask

How do I get the date from firestore timestamp?

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.

What is FieldValue in firebase?

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.

Is firestore timestamp UTC?

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.


1 Answers

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:

preview of Timestamp in the console

like image 142
mesqueeb Avatar answered Oct 11 '22 06:10

mesqueeb