Home > front end >  How to convert date to timestamp javascript
How to convert date to timestamp javascript

Time:09-23

Im trying to create a new document in firestore using a cloud function. However i am unable to save the date as a timestamp rather, it's saved as a string currently.

How it is right now

enter image description here

What i need to achieve

enter image description here

Snippet of my cloud function

    await admin.firestore()
    .doc('/buyerProfile/' response.buyerId)
    .collection('notifications')
    .add({
        'type':'New response',
        'responseId':response.responseId,
        'requestId':response.requestId,
        'date': Date(Date.now()),
        'compressedImgUrl':response.compressedImgUrl,
        'description':response.description,
        'requestPaidFor':request.data().isPaidFor
    }).then(()=>console.log('Notification created'));

CodePudding user response:

You can use to get timestamp new Date().getTime();

enter image description here


You can also use serverTimestamp() method instead:

date: admin.firestore.FieldValue.serverTimestamp()

CodePudding user response:

This worked for my case, i had to import Timestamp from firebase admin

const { timeStamp, time } = require("console");
...
'date': timeStamp.now(),
  • Related