2

I got timestamp in Firebase Functions with

admin.database.ServerValue.TIMESTAMP

However, when I pass it as a string, it becomes [object object] I saved it in Firebase Realtime database and when I checked it from console I saw 1505298866520 which I think it is milliseconds since UNIX epoch. Do you know how can I get string from it or how can I do time calculations with it? Or what type of object TIMESTAMP returns?

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Hamoonist
  • 2,286
  • 3
  • 19
  • 36
  • 1
    Cloud Functions runs regular JavaScript. So you question boils down to [how to convert a timestamp to a date string in JavaScript](https://www.google.com/search?q=how+to+convert+a+timestamp+to+a+date+string+in+JavaScript), which seems to lead to this answer: https://stackoverflow.com/questions/847185/convert-a-unix-timestamp-to-time-in-javascript – Frank van Puffelen Sep 13 '17 at 14:20

1 Answers1

3

The firebase.database.ServerValue.TIMESTAMP or admin.database.ServerValue.TIMESTAMP hold the following object {.sv: "timestamp"}. This is a specific firebase-database object. That tells the server it should use the server's UNIX-time when an object is added to the database.

That is the reason you can not use it as a date object in javascript.

If you use admin.database.ServerValue.TIMESTAMP on the server via cloud functions it should represent the same as new Date().getTime()

Jürgen Brandstetter
  • 7,066
  • 3
  • 35
  • 30