I am using Angularfire2 and I'm tesing to build a chat app that pushes messages in the database. I want to get the time when the data is pushed. Your help is really appreciated. Thanks a lot!
2 Answers
Firebase uses a constant value which is replaced with a numeric timestamp when it is written to the database. This removes the need to track and synchronize the time across clients.
firebase.database.ServerValue.TIMESTAMP
You can then read the value of the reference that was just written to see the exact time.
var sessionsRef = firebase.database().ref('sessions');
var mySessionRef = sessionsRef.push();
mySessionRef.update({ startedAt: firebase.database.ServerValue.TIMESTAMP });
mySessionRef.once('value').then(function(dataSnapshot) {
var time = dataSnapshot.child('startedAt'); // the time when the data was written
});
3 Comments
Lex Caraig
Yes, I saw this one, but how do you implement this in Angular 2 Typescript? Thanks!
Travis
This is just a value that you can include in any written data. I'm not sure I understand your question. Perhaps you can edit your question to describe exactly where you're stuck.
Ryan Langton
if you're using AngularFire2 you are using a wrapper around firebase and I don't see any access to ServerValue.TIMESTAMP either