Prathmesh Patil
Prathmesh Patil

Reputation: 287

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.

Upvotes: 20

Views: 72138

Answers (10)

Dhruv Sharma
Dhruv Sharma

Reputation: 1

Timestamp into Unixseconds

In Kotlin you can do like this:-

val time = document.get("date") as Timestamp
 Log.e("time", " ${time.seconds*1000}")

Output:-

1713551400000

Upvotes: 0

Judefabi
Judefabi

Reputation: 71

Within the Date() where you put your timestamp add .toDate()

to the timestamp variable as @jasonleonhard said. Maybe just an example

new Date(timestamp.toDate())

Upvotes: 0

Abdul
Abdul

Reputation: 21

I think its bit late but easiest way is just: (new Date(timestamp.toDate())).toDateString()

Upvotes: 0

jasonleonhard
jasonleonhard

Reputation: 13887

The .toDate() method should be all you need

You might like the docs here

As an added bonus, you might want very highly human readable output

Date only options

.toDate().toDateString()

.toDate().toLocaleDateString()

Time only options

.toDate().toTimeString()

.toDate().toLocaleTimeString()

Objects

However, if you are receiving an object you might do something like this

{JSON.stringify(createdAt.toDate()).replace(/['"]+/g, '')}

Converting the object into a string then replacing the quotes around the string.

Upvotes: 9

Rajendra singh rana
Rajendra singh rana

Reputation: 461

  • firebase time is basically combination of seconds and nano seconds time={ seconds:1612974698, nanoseconds:786000000 }

total_miliseconds=(time.seconds+(time.nanoseconds)*0.00000001)*1000. // 1 nanosecond=1e-9 means 0.00000001

new Date(total_miliseconds)

Upvotes: 4

Marcin Orlowski
Marcin Orlowski

Reputation: 75629

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));

Upvotes: 35

MrCoder
MrCoder

Reputation: 1

For date, you can use this code :

Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(time);
String date = DateFormat.format("dd-MM-yyyy", calendar).toString();

For time :

Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(time);
String date = DateFormat.format("hh:mm", calendar).toString();

Upvotes: 0

Favour Felix Chinemerem
Favour Felix Chinemerem

Reputation: 1408

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()

Upvotes: 18

JP Ventura
JP Ventura

Reputation: 5732

According to Firebase documentation, the types that are available JSON are:

  • String
  • Long
  • Double
  • Boolean
  • Map<String, Object>
  • List<Object>

Quoting another Stack Overflow post, I suggest you use JSON date string format yyyy-MM-dd'T'HH:mm:ss.SSSZ instead of epoch timestamp.

Comparing 1335205543511 to 2012-04-23T18:25:43.511Z, you can noticed that:

  • It's human readable but also succinct
  • It sorts correctly
  • It includes fractional seconds, which can help re-establish chronology
  • It conforms to ISO 8601

ISO 8601 has been well-established internationally for more than a decade and is endorsed by W3C, RFC3339, and XKCD

Upvotes: 6

Nay Linn
Nay Linn

Reputation: 21

String time=dataSnapshot.child("timeStamp").getValue().toString(); Long t=Long.parseLong(time);

Date myDate = new Date(t*1000);

Result


Fri May 11 05:37:58 GMT+06:30

Upvotes: 1

Related Questions