phantom
phantom

Reputation: 41

Writing the current Date and time to firebase

using this code with on button click listener

users.child(user.getUid()).child("Date").setValue(ServerValue.TIMESTAMP);

but i dont want it to be a TIMESTAMP like 1548105297646

Expected results:

From 1548105297646 to 19/1/2019 19:33:40

please check my Firebase database with expected results

Firebase database

Is there a code instead of (ServerValue.TIMESTAMP) ???

Upvotes: 0

Views: 1497

Answers (2)

Doug Stevenson
Doug Stevenson

Reputation: 317382

In Realtime Database, if you time ServerValue.TIMESTAMP, you will always write a number measured in milliseconds since the unix epoch. You can't change this behavior.

If you want to write a string to the database, then compose the string yourself and write that value. But I will suggest that writing the number is a much better option, since they're easier to search and sort in the database. You should probably format that date value on the client instead. Java (and Android in particular) have great date and time formatting libraries you can use.

Upvotes: 1

Mocas
Mocas

Reputation: 1660

No, you can't directly write the server time to firebase in DateTime format.

You have one of two options:

1 - Write server time as timestamp, then when you read it when you want to use it do the conversion(that's what I do)

2 - If IT REALLY NEEDS TO BE STORED IN DateTime format, then you can write the server time stamp, read it, convert it, write it as DateTime.

Upvotes: 1

Related Questions