0
votes

I want to get Firebase Server current time as a timestamp but the value returned is wrong. The server returns a timestamp = 1535916111663 and if we convert this timestamp to date, I will be getting 30 March 50641 at 21:21:03

Note: I am using firebase Cloud function.

  exports.addPost = functions.region('europe-west1').https.onRequest((request, response) => {

  admin.database().ref('/.info/serverTimeOffset').once('value').then(function stv(data) {
          console.log(data.val() + Date.now());
    }, function (err) {
        return err;
   });
})
1
Why are you using serverTimeOffset on Cloud Functions? All Google servers (including those handling Realtime Database and Cloud Functions) all have exactly the same server date. There should be no need for offset. - Doug Stevenson
i found this function on stackoverflow so i used it - Firas Chebbah
Just use Date.now() if you want an accurate timestamp in Cloud Functions. - Doug Stevenson
even Date.now() returns a value in year 50641 - Firas Chebbah
What are you using to determine that? - Doug Stevenson

1 Answers

3
votes

Whatever it is you're using to convert that timestamp into a date, it's interpreting the value as a number of seconds since the unix epoch. However, the value you're getting is actually measured in number of milliseconds since the unix epoch. You'll need to use a conversion tool that converts a timestamp in milliseconds to a date.

If you paste copy the value into this tool, it will automatically take the value as milliseconds. The value "1535916111663" that you provided above converts to "GMT: Sunday, September 2, 2018 7:21:51.663 PM"