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粉9394737592024-03-31 14:40:55
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.