Mahesh
Mahesh

Reputation: 1603

Firestore timestamp - On passing as props becomes Object

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

Answers (2)

Frank van Puffelen
Frank van Puffelen

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

Mahesh
Mahesh

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

Related Questions