Reputation: 1603
On passing firestore's date timestamp on another Component, the date timestamp becomes an object.
to show the date, all we need is date.toDate()
but once i passed it to another component, i am not able to display the date by using date.toDate()
. Because it becomes an object. An object with seconds and nanoseconds.
Why does it happen?
Is that how it should work?
Do i have to convert the seconds to milliseconds and use it in javascript's new Date
object?
Upvotes: 0
Views: 141
Reputation: 599946
When you retrieve a date field from Cloud Firestore, you get a Timestamp
object. To convert that to a regular JavaScript Date
object, you can call its toDate()
method.
So in your example:
date.toDate().toDateString()
Although I would recommend renaming the variable to timestamp
, or something similar, to prevent future confusion about its type.
Upvotes: 1
Reputation: 1603
I don't know why does timestamp get converted to an object on passing to a component.
Right now i am using the following way to show the date.
new Date(date.seconds * 1000).toDateString()
Upvotes: 0