date-fns considers invalid a date from fromUNIXTime()

34 Views Asked by At

I'm trying to work with a converted timestamp from firebase using date-fns. I'm flattening out the records I get back from firestore so:

    const result = Object.keys(docSnap.data()).map((key) => ({
      id: key,
      ...docSnap.data()[key],
    }));

and then changing the format of the time stamp I receive like this:

    result.forEach((element) => {
      element.startDateTime = fromUnixTime(element.startDateTime.seconds);
      element.endDateTime = fromUnixTime(element.endDateTime.seconds);
    });

this is how an object looks like after:

[
    {
        "id": "1",
        "startDateTime": "2024-02-08T12:00:00.000Z",
        "endDateTime": "2024-02-08T12:30:00.000Z",
        "name": "Anna Karenina"
    }
]

I'm using these objects as a react state variable (useState). when I pass them to the child component, and I test the elements, the isValid() function returns true. But if I try to use this value in isSameDay() I only get false returns, because date-fns doesn't consider it a valid date.

same if I use format(), I get an Invalid Date error

the most confusing part is that in a single console.log() the value can be parse and be undefined at the same time. this code:

console.log('is meeting.startDateTime valid?: ', isValid(meeting.startDateTime), ' ,meeting.startDatetime: ', meeting.startDatetime, ', day: ', day, ', isSameDay: ', isSameDay(meeting.startDatetime, day))
                     

returns:

is meeting.startDateTime valid?:  true  ,meeting.startDatetime:  undefined , day:  Sun Feb 25 2024 00:00:00 GMT+0100 (Central European Standard Time) , isSameDay:  false

how can I make the comparison of both dates?

0

There are 0 best solutions below