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
  • 499,950
  • 69
  • 739
  • 734
Hamoonist
  • 1,907
  • 1
  • 18
  • 35
  • 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
  • 6,226
  • 3
  • 33
  • 30