skip to Main Content

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


  1. Chosen as BEST ANSWER

    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]

    enter image description here

    Now, about the date formats I did some changes:

    Having disabled the serializeCheck option, now I can use .toDate() functions

    enter image description here

    Now wherever I need a date in a specific format I simply use a global function that receives two params: date & format and using momentJS the rest is easy.

    enter image description here


  2. ****we can use this format ….. work for me ****

    import moment from 'moment';  <--- import this line 
    
    const timestamp = 1676238124;
    const nanoseconds = 838000000;
    
    
    const Date = moment.unix(timestamp).add(nanoseconds / 1000000, 'milliseconds');
    const Fordate = Date.format('YYYY-MM-DD HH:mm:ss.SSS');
    console.warn(Fordate)
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search