Im fetching some data from firestore, the date fields are in the format of {seconds: XXXXX, nanoseconds: XXXXX}
. React Native complains about this saying this format is not a serializable value so to those fields I do this:
docData.createdAt.toLocaleString()
That gives me dates in the following format:
Timestamp(seconds=13223213, nanoseconds=12312312)
When I want to render those date fields with a human format like ISOString or LocaleDateString or anything it doesnt let me.
I’ve tried to do new Date(createdAt)
but that gives me NaN.
I’ve also tried createdAt.toDate() | createdAt.toISOString()
it doesnt recognize the functions.
I’ve also tried Timestamp.fromDate(createdAt) but it doesnt work either.
I want to get my dates in a human reading format like DD/MM/YYYY HH:mm:ss
Here some pieces of code:
Fetching data:
Some of my attempts
I’d like to have some hints or ideas or how to approach this issue.
2
Answers
Although the above answer works, this is what I did:
The issue was caused because of the SerializableCheck from redux [https://redux-toolkit.js.org/api/serializabilityMiddleware] so I decided to set this middleware to false so It won't cause this error.
This is a StackOverFlow question about this topic that I found [https://stackoverflow.com/questions/61704805/getting-an-error-a-non-serializable-value-was-detected-in-the-state-when-using]
Now, about the date formats I did some changes:
Having disabled the serializeCheck option, now I can use .toDate() functions
Now wherever I need a date in a specific format I simply use a global function that receives two params:
date & format
and usingmomentJS
the rest is easy.****we can use this format ….. work for me ****