A way to get time from Firebase is using Firebase.ServerValue.TIMESTAMP
when you .push()
a message.
Like this myDataRef.push({name: name, text: text, time: Firebase.Server.TIMESTAMP});
Due to Firebase Timestamp is the time since the UNIX epoch, in milliseconds, you need to transform it before use it. This function might help you with that:
time = function (timestamp) { // Convert UNIX epoch time into human readble time.
var epoch = new Date(timestamp);
var date = epoch.toUTCString();
return date;
}
Finally, your display logic might be something like this:
myDataRef.on('child_added', function(snapshot) {
var message = snapshot.val();
var t = time(message.time);
displayChatMessage(message.name, message.text, t);
});