Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Firebase TIMESTAMP to date and Time

I am using firebase for my chat application. In chat object I am adding time stamp using Firebase.ServerValue.TIMESTAMP method.

I need to show the message received time in my chat application using this Time stamp .

if it's current time i need to show the time only.It's have days difference i need to show the date and time or only date.

I used the following code for convert the Firebase time stamp but i not getting the actual time.

var timestamp = '1452488445471'; var myDate = new Date(timestamp*1000); var formatedTime=myDate.toJSON(); 

Please suggest the solution for this issue

like image 993
Kichu Avatar asked Jan 11 '16 10:01

Kichu


People also ask

How do I change firestore timestamp to date?

To convert a Firestore date or timestamp to a JavaScript Date, we use firebase. firestore. Timestamp. fromDate to convert the a date to a Firestore timestamp.

What is timestamp firebase?

firestore. Timestamp. 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.

How do I convert time stamps to dates?

The constructor of the Date class receives a long value as an argument. Since the constructor of the Date class requires a long value, we need to convert the Timestamp object into a long value using the getTime() method of the TimeStamp class(present in SQL package).

What is ServerValue timestamp?

ServerValue. TIMESTAMP in data that's written to the Firebase database. It's a placeholder for the actual time on the server and will be replaced when the server receives and writes the data.


1 Answers

Firebase.ServerValue.TIMESTAMP is not actual timestamp it is constant that will be replaced with actual value in server if you have it set into some variable.

mySessionRef.update({ startedAt: Firebase.ServerValue.TIMESTAMP }); mySessionRef.on('value', function(snapshot){ console.log(snapshot.val()) }) //{startedAt: 1452508763895} 

if you want to get server time then you can use following code

  fb.ref("/.info/serverTimeOffset").on('value', function(offset) {     var offsetVal = offset.val() || 0;     var serverTime = Date.now() + offsetVal;   }); 
like image 192
Bek Avatar answered Sep 22 '22 23:09

Bek