How do I get the server timestamp in Cloud Functions for Firebase?

前端 未结 7 1200
轮回少年
轮回少年 2020-12-03 00:12

I know you can pull the server timestamp in web, ios, and android - but what about the new Cloud Functions for Firebase? I can\'t figure out how to get the server timestamp

7条回答
  •  Happy的楠姐
    2020-12-03 00:53

    Using the following in the cloud function which is equivalent of Timestamp.now() on client side, this returns the current Timestamp

    admin.firestore.Timestamp.now()
    

    But if you want to initialise Timestamp from Date object you can do it as follows

    admin.firestore.Timestamp.fromDate(new Date())
    

    And if you want to to initialise Timestamp for future or past date then first intialise the Date object either from parsing string or setting time that you want to set and pass it to Timestamp.fromDate()

    var date = new Date('Wednesday, October 30, 2019 09:10 PM')
    //or date = new Date('2014-06-30T06:40:53+05:30')
    var timestamp = admin.firestore.Timestamp.fromDate(date)
    

提交回复
热议问题