0

I'm trying to populate a table from a firestore database which has field value timestamp data:

https://i.stack.imgur.com/LJCi4.png

with the following code for web:

firebase.initializeApp(firebaseConfig);

const attendanceCollection = firebase.firestore().collection("attendance");

var lastIndex = 0;

function getData() {
    var htmls = [];
    attendanceCollection.get().then(querySnapshot => {    
        querySnapshot.forEach(doc => {

            console.log(`${doc.id}`);
            console.log(`${(doc.data().GENERALASSEMBLY)}`)

            htmls.unshift('<tr>\
                <td>'+ `${doc.id}` +'</td>\
                <td class="message" style=" text-overflow: ellipsis">'+ `${(doc.data().GENERALASSEMBLY)}` +'</td>\
                <td style="overflow: hidden; text-overflow: ellipsis">'+ `${doc.id}` +'</td>\
            </tr>');
        });
        $('#tbody').html(htmls);
    $("#submitUser").removeClass('desabled');
    });

};

which returns this: https://i.stack.imgur.com/VvsvM.png

I've found some references but never actually helped me solve what I'm trying to make:

How to get a JavaScript Date object from the new `firebase.firestore.FieldValue.serverTimestamp()`

How do I convert a Firestore date/Timestamp to a JS Date()?

how do I implement the toDate() in my code above? can you give me samples of using toDate() on converting firestore timestamps to date/string?

Hasta Dhana
  • 4,699
  • 7
  • 17
  • 26
Efren Pineda
  • 27
  • 1
  • 8

2 Answers2

2

You'll need to call toDate() on the GENERALASSEMBLY field to get its value as a date. And since the field will initially be null when the first event fires, you also need to detect if GENERALASSEMBLY is null first.

${(doc.data().GENERALASSEMBLY ? doc.data().GENERALASSEMBLY.toDate() : "-unknown-")}
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
1

if you are using the library date-fns remember that it is unix time so fromUnixTime(GENERALASSEMBLY.seconds)

then you can just use the format and parse the fromUnixTime

format(fromUnixTime(GENERALASSEMBLY.seconds))

Cyrus Zei
  • 2,589
  • 1
  • 27
  • 37