Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert firebase timestamp into date and time

I have tried to get date and time from firebase timestamp as follows:

 Date date=new Date(timestamp*1000);
 SimpleDateFormat sfd = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
 sfd.format(date);

but I'm getting results like:

:02-02-48450 04:21:54
:06-02-48450 10:09:45
:07-02-48450 00:48:35

as you can see the year is not as we live.

So, please help me to fix this.

like image 493
Prathmesh Patil Avatar asked Jun 24 '16 14:06

Prathmesh Patil


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.

Is firestore timestamp UTC?

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 get firebase time?

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/').


2 Answers

Your timestamp 1466769937914 equals to 2016-06-24 12:05:37 UTC. The problem is that you are multiplying the timestamp by 1000. But your timestamp already holds a value in milliseconds not in seconds (this false assumption is most likely the reason you have the multiplication). In result you get 1466769937914000 which converted equals to 48450-02-01 21:51:54 UTC. So technically speaking all works fine and results you are getting are correct. All you need to fix is your input data and the solution is quite simple - just remove the multiplication:

SimpleDateFormat sfd = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
sfd.format(new Date(timestamp));
like image 179
Marcin Orlowski Avatar answered Sep 22 '22 19:09

Marcin Orlowski


If you are looking to get a Date instance from Timestamp

If you need to get just the Date object from Timestamp, the Timestamp instance comes with a toDate() method that returns a Date instance.

For clarity:

Date javaDate = firebaseTimestampObject.toDate()
like image 38
Favour Felix Chinemerem Avatar answered Sep 21 '22 19:09

Favour Felix Chinemerem