Using date-fns with Firestore server timestamps
P粉035600555
P粉035600555 2024-03-30 17:37:10
0
1
388

I'm trying to format my Firestore server timestamp in React Native using https://date-fns.org/

I found this example function from the documentation

formatDistance(subDays(new Date(), 3), new Date(), { addSuffix: true })

I assume formatDistance has two parameters. The date I calculate is also the current date. (Calculate distance)

However, when using:

formatDistance(subDays(new Date(), item.created_at.toDate()), new Date(), { addSuffix: true, })

(item.created_at) - is my timestamp.

I get the error:

Invalid time value

P粉035600555
P粉035600555

reply all(1)
P粉939473759

For subDays based on this document , the required parameters are the date (the day to change) and the amount (the number of days to subtract the first parameter). subDays requires a number for the second argument, but you provided it with a date. You can use formatDistance without subDays.

In your use case this should be enough.

formatDistance(new Date(item.created_at.toDate()), new Date(), { addSuffix: true, })

As long as item.created is in the correct format, this should work. This is the formatDistance documentation.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!