I'm working on a web project with Firebase. I call:
firebase.database.ServerValue.TIMESTAMP
and it returns:
{.sv: "timestamp"}
How will I get time of Firebase server with javascript?
ServerValue. TIMESTAMP returns a non-null Object and is a placeholder value for auto-populating the current timestamp. It doesn't contain the actual timestamp. The database will replace this placeholder when it will execute the command. It works as you expect when you are using it inside a database.
Date and time in the Firestore are saved in their proprietary date format — called Timestamp. Firestore timestamp is just a simple object with two properties — seconds and nanoseconds, which apparently has some advantages over “classic” date format.
To reach the timestamp of firebase server on client, you first need to write the value to the server then read the value. firebase. database(). ref('currentTime/').
What is timestamp in firebase? A Timestamp represents a point in time independent of any time zone or calendar, represented as seconds and fractions of seconds at nanosecond resolution in UTC Epoch time. It is encoded using the Proleptic Gregorian Calendar which extends the Gregorian calendar backwards to year one.
This is what I had to do:
firebase.firestore.FieldValue.serverTimestamp()
Source:
https://firebase.google.com/docs/reference/js/firebase.firestore.FieldValue
Just to add some more to answer from @cartant:
admin.database.ServerValue.TIMESTAMP
returns a non-null Object and is a placeholder value for auto-populating the current timestamp. It doesn't contain the actual timestamp. The database will replace this placeholder when it will execute the command.
It works as you expect when you are using it inside a database.ref
but outside of it is just an useless Object.
See this SO Answer for more.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With